-1

I have a mutable array (editedServiceParts) with two objects in it; I have a table view with about 30 cells in it. I want to check to see if any of table view cells (cell.textLabel.text) appear in the mutable array; if they do, I want to set the cell.accessory to checkmark.

UPDATED This is my code from -cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

//  get the timeFormat
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy];
int iTimeFormat = [[preferencesDict objectForKey:@"timeFormat"] intValue];  //  set timeFormat
NSMutableArray *selectedServicesParts = [NSMutableArray new];
NSMutableArray *editedServiceParts = [NSMutableArray new];
NSString *service;



if(tableView.tag == kServicesTableView) {  //  services array

    static NSString *CellIdentifier = @"apptServicesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell
    SingletonServicesArray *sharedServicesArray = [SingletonServicesArray sharedServicesArray];  //  list of available services
    [cell.textLabel setText:[sharedServicesArray.globalServicesArray objectAtIndex:indexPath.row]];  //  move selected row to cell

    //  separate soServices.text into individual elements
    NSString *cleanService;
    if(soServices.text.length > 0)  {
        selectedServicesParts = [[soServices.text componentsSeparatedByString:@","] mutableCopy];

        for(int x = 0; x < selectedServicesParts.count; x++)  {
            cleanService = [selectedServicesParts[x] stringByReplacingOccurrencesOfString:@"," withString:@""];

            [editedServiceParts insertObject: cleanService atIndex: x];
        }
    }

    //  now, take the editedServicesParts (w/o commas) and mark cell if appropriate
   for (int i = 0; i < editedServiceParts.count; i++) {   if ([editedServiceParts containsObject:cell.textLabel.text]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        // Remove accessory mark of recycled cells
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
   }
//        for (int i = 0; i < editedServiceParts.count; i++) {  //  total number of rows in list of all available services
//            
//            for(service in editedServiceParts) {
//                if([cell.textLabel.text isEqualToString: service])  {
//                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
//                    break;
                }
//            }
//        }

    return cell;

}

What's happening is only the first comparison is being found, although there is another that should have been found. I fear I have a logic problem here, and for the life of me, I don't see it. Help would be greatly appreciated!

SD

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

1 Answers1

0

You can use containsObject: method to avoid looping altogether:

if ([editedServiceParts containsObject:cell.textLabel.text]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    // Remove accessory mark of recycled cells
    cell.accessoryType = UITableViewCellAccessoryNone;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Your code still only gives me the first match, ignoring the other matches... and I forgot to mention, this code is in -cellForRowAtIndexPath... sorry – SpokaneDude Apr 01 '15 at 20:39
  • When the user selects a row from the tableViewCell, it is copied to a textField, which is then copied to the CD store. When the row is selected from the CD store, it is copied to the textField, where it is used in the compare after editing out commas, etc... (sure hope that makes sense)... – SpokaneDude Apr 01 '15 at 20:49
  • you are absolutely right about the match; the line of code that removes the comma (I modified it to be @", ") somehow isn't working (a space gets inserted in all of the objects except the first one... I'll work on that... thank you so much for your time... I really, really appreciate it (and learned something also -- containsObject)... – SpokaneDude Apr 01 '15 at 21:10