0

我的有几个有线问题UITableView

  1. 更改那些的单元格背景颜色indexPath.row = 0。但有些细胞也发生了变化。它看起来很随机。
  2. 两种创建方法,但很奇怪的是......,太复杂UITableViewCell,请看SMFirstViewController.mgithub 。

  3. prepareForSegue:sender:没有按预期工作。奇怪的是,indexPath = [self.tableView indexPathForCell:sender]有时indexPathnull根据细胞方法 1 或 2

这些问题看起来太复杂了,所以我把我的代码上传到了 github。https://github.com/i0sdev/Temp/tree/segueSMFirstViewController.m发生问题的文件。

如果您能帮忙解决,非常感谢!谢谢!

4

1 回答 1

1

您的问题是您没有考虑方法 1 中的单元重用(您甚至不应该使用方法 2)。执行 if 语句后,您的单元格将变为棕色,并且当该单元格被重用时,无论它位于什么 indexPath.row 中,它仍然是棕色的。每当您像您一样根据 indexPath 设置单元格的某些属性时这样做,您需要有一个 else 子句,而不仅仅是一个 if。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (indexPath.row == 0)
    {
        cell.backgroundColor = [UIColor brownColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }else{
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
    }
    NSArray * array = [[list objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = [array valueForKey:@"Number"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"row: %d", indexPath.row];
    
    return cell;
}

只要您坚持方法 1,那么 prepareForSegue 应该可以正常工作。

于 2013-09-22T05:13:01.113 回答