我的夫妇有一些有线问题 UITableView
indexPath.row = 0
。但是有些单元格也发生了变化。它看起来随机。两种方法来创建UITableViewCell
的,但很奇怪的是......,太复杂,请参阅SMFirstViewController.m
在github上。
prepareForSegue:sender:
不能按预期工作。奇怪的是,indexPath = [self.tableView indexPathForCell:sender]
有时indexPath
是null
关于细胞方法1或2
这些问题看起来太复杂了,因此我将代码上传到github上。https://github.com/i0sdev/Temp/tree/segue,SMFirstViewController.m
就是问题发生的文件。
非常感谢您能帮助解决问题!谢谢!
您的问题是您没有考虑方法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应该可以正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句