【问题标题】:Fast-enumeration logic error checking for compare of UITableViewCellUITableViewCell 比较的快速枚举逻辑错误检查
【发布时间】:2015-04-01 20:24:56
【问题描述】:

我有一个可变数组 (editedServiceParts),其中包含两个对象;我有一个包含大约 30 个单元格的表格视图。我想检查是否有任何表格视图单元格 (cell.textLabel.text) 出现在可变数组中;如果他们这样做,我想将 cell.accessory 设置为复选标记。

已更新这是我来自 -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;

}

所发生的只是找到了第一个比较,尽管应该找到另一个比较。我担心我这里有一个逻辑问题,而对于我的生活,我看不到它。非常感谢您的帮助!

标清

【问题讨论】:

  • 为什么需要外循环,多次重复同一个任务?
  • 您的内部和外部循环都在迭代editedServiceParts。您是否打算让内部循环遍历您的表格视图单元格?
  • cell 永远不会改变,因此您会不断检查相同的单元格文本。
  • 嗯...此代码在 -cellForRowAtIndexPath... 我可能应该提到...对不起...
  • 你能用完整的cellForRowAtIndexPath代码更新你的问题吗?

标签: objective-c uitableview ios8.1 fast-enumeration


【解决方案1】:

您可以使用containsObject: 方法来完全避免循环:

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

【讨论】:

  • 您的代码仍然只给了我第一个匹配项,忽略了其他匹配项...我忘了提,这段代码在 -cellForRowAtIndexPath... 抱歉
  • 当用户从 tableViewCell 中选择一行时,它被复制到一个 textField,然后被复制到 CD 存储。当从 CD 存储中选择行时,它被复制到 textField,在编辑掉逗号等后用于比较...(当然希望有意义)...
  • 你对比赛的看法是绝对正确的;删除逗号的代码行(我将其修改为@“,”)不知何故不起作用(除了第一个对象之外的所有对象中都插入了一个空格......我会努力的......非常感谢您抽出宝贵的时间...我真的非常感谢它(并且还学到了一些东西 - containsObject)...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多