0

I have allSpecialities array, and selectedSpecialities array. I'm downloading those arrays from the server, parse them to object and adding them to those arrays. Now, I want to check/uncheck some specialities. I've tried with containsObject, but that's not working, because those objects are not on the same memory location. This is code that I've done so far, and it's working, but I have problem how to add them to this array. in cellForRowAtIndexPath:

for (Speciality *specTemp in self.selectedSpecialities) {
        if (speciality.specialityId == specTemp.specialityId) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            break;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

in didSelect:

Speciality *speciality = [[Speciality alloc]init];
    speciality = self.allSpecialities[indexPath.row];
    NSMutableArray *tempSelectedSpecialities = [[NSMutableArray alloc]initWithArray:self.selectedSpecialities];
    int i=0;
    for (Speciality *tempSpeciality in tempSelectedSpecialities) {
        if (tempSpeciality.specialityId == speciality.specialityId) {
            [self.selectedSpecialities removeObjectAtIndex:i];
        }
        else {

        }
        i++;
    }

    [self.delegate companySpecialities:self.selectedSpecialities];
    [self.specialitiesTableView reloadData];
8
  • Beginners mistake: In didSelect, you create a Speciality object, and then immediately overwrite it. Don't do that. Commented Jan 12, 2016 at 9:44
  • Can you explain me how to do that on other way? Commented Jan 12, 2016 at 9:47
  • Speciality *speciality = self.allSpecialities[indexPath.row]; like this. Commented Jan 12, 2016 at 9:48
  • Ok, but that not solved my problem. Commented Jan 12, 2016 at 9:48
  • 1
    If you want containsObject: to work, you need to implement isEqual:, and I think, hash. Commented Jan 12, 2016 at 9:50

1 Answer 1

3

I have declare one Mutable array in .h to store a data

 NSMutableArray *selectedMarks;

assign memory in viewDidLoad

selectedMarks = [NSMutableArray new];

add and remove object in didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   TableViewCell *cell = (TableViewCell *)[self.tblview cellForRowAtIndexPath:indexPath];


    NSString *strIndex=[NSString stringWithFormat:@"%ld",(long)indexPath.section];

    if ([selectedMarks containsObject:strIndex])// Is selected?
    {
        [selectedMarks removeObject:strIndex];
    }
    else{
        [selectedMarks addObject:strIndex];
        }
}

in cellForRowAtIndexPath

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CRTableViewCellIdentifier = @"TableViewCell";
        TableViewCell *cell = (TableViewCell *)[self.tblview dequeueReusableCellWithIdentifier:CRTableViewCellIdentifier];
        if (cell == nil) {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CRTableViewCellIdentifier];
        }
        cell.backgroundColor=[UIColor colorWithRed:122/255.0 green:196/255.0 blue:251/255.0 alpha:1];

        // Check if the cell is currently selected (marked)
           NSString *txtQRCodeid = [[[dictListQR objectForKey:@"boxlist"]objectAtIndex:indexPath.section]valueForKey:@"qr_code_id"];

NSString *text1 = [[[dictListQR objectForKey:@"boxlist"]objectAtIndex:indexPath.section]valueForKey:@"box_name"];
        NSString *text=[NSString stringWithFormat:@"QR Code %ld with %@",indexPath.section+1,text1];

         cell.isSelected = [selectedQR containsObject:txtQRCodeid] ? YES : NO;
        if (cell.isSelected) {
            [cell.btnSelection setImage:[UIImage imageNamed:@"check"] ];
            // set image of selected
        }
        else{
            [cell.btnSelection setImage:[UIImage imageNamed:@"uncheck"] ];
            // set unselected image
        }
     cell.lblQRcodeText.text=text;
   cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        return cell;
       }

It's Work for me

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.