我用核心数据中的数据加载一个 TableView 。
它加载了文本。
每个单元格上都有一个 uiimageview。当表格调用 cellForRow:
时,它要么将图像设置为隐藏,要么不隐藏(取决于核心数据表明它应该是什么。)
代码:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString * identifier = @"identifier";
self.cell = [tableView dequeueReusableCellWithIdentifier:identifier];
HuntingRifleGuns *handguns = [self.tableArray objectAtIndex:indexPath.row];
NSString *brandModel = [[handguns.brand stringByAppendingString:@" "] stringByAppendingString:handguns.model];
self.cell.mainLabel.text = brandModel;
self.cell.nicknameLabel.text = handguns.nickname;
//Right below me, is where is set the imageView to hidden, or not hidden.
self.cell.alert.hidden = handguns.showBadge.boolValue;
self.cell.alert.image = [UIImage imageNamed:@"tvicon.png"];
return self.cell;
}
我的问题是:如果我在表格 View 上有 1 个单元格,它工作完美,但如果我在表格上有超过 1 个单元格,它就会起作用。
我想检查单元格的图像在被删除时是否被隐藏:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (!self.cell.alert.isHidden) {
NSLog(@"showing");
}
else{
NSLog(@"Not showing");
}
.........
但是当它测试时,它并不总是做正确的事情。它只是随机打印出 showing
或 not showing
。而且我知道它是否应该显示,因为它会在单元格上显示图像。这可能是什么原因?
顺便提一下,用户可以将图像设置为隐藏或不隐藏在不同的 View 中,但 tableview 始终显示正确的数据,这意味着它显示图像正确可见或隐藏,就在我测试它时,它并不总是工作。
感谢您的帮助!
最佳答案
您已经创建了一个名为cell
的@property
。那行不通的。
您应该只使用一个 UITableViewCell
变量或一个与您的自定义单元格一起使用:
CustomCell *cell = [tableView dequeue...];
此外,您对字符串的处理在内存上的效率不是很高。使用 stringWithFormat
代替:
cell.mainLabel.text = [NSString stringWithFormat:@"%@ %@",
handguns.brands, handguns.model];
此外,检查 View 是否隐藏以通知您的应用程序逻辑是非常糟糕的做法。相反,您应该拥有一个健壮的数据模型并改为查询数据模型。在您的情况下,您应该查询适当的 handguns
对象是否有图像。
关于iphone - UITableView 单元格提供错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603740/