2015-04-01 57 views
-1

我有一个可变数组(editedServiceParts)与其中的两个对象;我有一个约30个单元格的表格视图。我想检查是否有任何表视图单元格(cell.textLabel.text)出现在可变数组中;如果他们这样做,我想将cell.accessory设置为复选标记。快速枚举逻辑错误检查比较UITableViewCell

修订这是-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; 

} 

发生了什么事是只有第一个比较被人发现,虽然有另一个应该已经被发现。我担心这里有一个逻辑问题,而对于我来说,我没有看到它。帮助将不胜感激!

SD

+0

为什么你需要外循环,重复相同的任务多次? – dasblinkenlight 2015-04-01 20:32:29

+0

内部和外部循环都在遍历editedServiceParts。你是否打算让内部循环迭代你的表格视图单元格? – DanielG 2015-04-01 20:32:59

+0

'cell'永远不会改变,所以你不断检查相同的单元格文本。 – 2015-04-01 20:39:03

回答

0

您可以使用containsObject:方法来避免循环干脆:

if ([editedServiceParts containsObject:cell.textLabel.text]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    // Remove accessory mark of recycled cells 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

您的代码仍然只给我第一场比赛,忽略其他比赛...我忘了提及,这段代码是在-cellForRowAtIndexPath ...对不起 – SpokaneDude 2015-04-01 20:39:07

+0

当用户从tableViewCell中选择一行时,它被复制到textField,然后将其复制到CD商店。当从CD商店中选择该行时,它被复制到textField中,在编辑出逗号等后用于比较...(当然,希望有道理)... – SpokaneDude 2015-04-01 20:49:31

+0

你绝对是对的比赛;删除逗号(我将其修改为@“,”)的代码行不起作用(一个空格被插入到除第一个之外的所有对象中......我将继续处理这个...非常感谢你的时间...我真的很感激它(还学到了一些东西 - containsObject)... – SpokaneDude 2015-04-01 21:10:57