我有这个来了几次,每次我从来没有找到一个“优雅”的解决方案。自定义更改的UITableViewCell文本颜色
问题: 我有一个客户UITableViewCell
与XIB
,InventoryCustomCell
。该单元格有UILabels
。 UILabel
的默认文字颜色为黑色。我有一行索引数组,其中UILabel
文本颜色需要灰色。我在我的InventoryCustomCell
中有一个公开的方法,它允许我设置UILabel
的颜色。
公共方法是这样的:
- (void)setCellAdded:(BOOL)cellAdded {
UIColor *cellTextColor;
if (cellAdded) {
self.thumbImageView.alpha = 0.5f;
cellTextColor = [UIColor lightGrayColor];
} else {
self.thumbImageView.alpha = 1.0f;
cellTextColor = [UIColor blackColor];
}
self.titleLabel.textColor = cellTextColor;
self.partNumberLabel.textColor = cellTextColor;
self.priceLabel.textColor = cellTextColor;
self.quanityLabel.textColor = cellTextColor;
self.addButton.titleLabel.textColor = cellTextColor;
}
在我UITableViewController
类,我注册与XIB
类。
[self.inventoryListTable registerNib:[UINib nibWithNibName:@"InventoryCustomCell" bundle:nil] forCellReuseIdentifier:@"InventoryCustomCellID"];
在我cellForRowAtIndexPath
我设置这样的:
InventoryCustomCell *cell = (InventoryCustomCell *)[tableView dequeueReusableCellWithIdentifier:inventoryCellID forIndexPath:indexPath];
.....
Product *product = .. is grabbed from NSFetchedResultsController
if ([addedProducts containsObject:product.objectID])
[cell setCellAdded:YES];
else
[cell setCellAdded:NO];
return cell;
现在,如果我在setCellAdded
添加断点if
块是为了将小区设置标签颜色为灰色输入正确。所以我知道这实际上被称为。
我觉得这里的问题是什么,是表视图试图重用细胞,因为它们都具有相同的标识符,它不现在哪些应该是灰色的,哪些不应该。但如果是这种情况,那么我希望看到一些细胞随机灰色,有些不在setCellAdded
之后被称为第一次。事实并非如此。细胞没有一个变成灰色,但是把它们变成灰色的呼叫始终是灰色的。
如果我使用的是默认的UITableViewCell
s,我可能只是为黑色/灰色的单独的cellIdentifier
。由于我使用的自定义UITableViewCell
有一个笔尖,我必须注册一个cellIdentifier
我不能使用这种方法,而只保留一个XIB
文件。
我还以为会工作的唯一的事情是,如果我创建了一个新的厦门国际银行有相同小区的看法,但添加的所有标签是灰色的。然后我可以使用两个cellIdentifier
的方法,但这只是看起来很诡异。
这些单元格在黑色文本表中显示吗? – Justin
是的,他们确实显示为黑色 – random
如果您不执行registerNib方法会发生什么?如果你使用'会发生什么[一个NSBundle mainBundle] loadNibNamed:@“InventoryCustomCell]' – Justin