单元格复选标记奇数一个变为蓝色,而其他变为UIColor

时间:2014-01-19 03:25:17

标签: ios objective-c uitableview

我的UITableView目前使用自定义绿色作为复选标记附件,我只需使用[[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:@"#669900"]];即可设置。这样可以正常工作,除了总是有一个单元格重复(b / c的单元重用),它保持默认的蓝色。我不知道为什么会发生这种情况,如果有人知道你能做些什么来防止我遇到这个“奇怪的人”问题,请分享。这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    #define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?@"":str

    static NSString *CellIdentifier = @"inviteCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:@"#669900"];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.backgroundColor = [UIColor blackColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    [[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:@"#669900"]];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] init];
    }

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

    BOOL isSearching = tableView != self.tableView;
    NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
    id p = arrayToUse[indexPath.row];

    NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
    NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];

    BOOL showCheckmark = [[stateArray objectAtIndex:indexPath.row] boolValue];
    if (showCheckmark == YES)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        NSLog(@"It hit showCheckmark = YES, and stateArray is %@",stateArray[indexPath.row]);
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        NSLog(@"It hit showCheckmark = NO, and stateArray is %@",stateArray[indexPath.row]);
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    id object = contactsObjects[indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [stateArray insertObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row];
        [selectedObjects addObject:object];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [stateArray insertObject:[NSNumber numberWithBool:NO] atIndex:indexPath.row];
        [selectedObjects removeObject:object];
    }

    //slow-motion selection animation.
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

2 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。

  1. 您正在多次设置外观选择器。这些只需要调用一次来设置特定属性。您可能应该在AppDelegate或在应用启动时调用的其他地方执行此操作。
  2. 这是有问题的一行:

    [[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:@"#669900"]];
    

    解决此问题后,您发布的问题实际上将得到解决。之所以发生这种情况,是因为外观选择器仅用于新初始化的对象。 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath不仅会使一个单元格出列,而且如果没有任何要出列的内容,也会初始化一个单元格。这意味着在设置外观选择器之前,在没有你想要的tintColor的情况下创建一个单元格。

    1. 正如我之前提到的,您调用的方法实际上会根据需要创建一个单元格。这意味着所有这一切:

      if(cell == nil){     cell = [[UITableViewCell alloc] init]; }

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

    2. 永远不会被召唤,所以你也可以摆脱它。

答案 1 :(得分:0)

当你获得一个新的细胞时,你的逻辑错了。在检查是否有单元格之前,先设置一些单元属性。检查'nil',必要时换一个新细胞,然后弄乱你的细胞。

相关问题