1

我遇到过几次这种情况,每次都找不到“优雅”的解决方案。

问题: 我有一个客户UITableViewCellXIB, InventoryCustomCell。那个细胞有UILabels。的UILabel默认文本颜色为黑色。UILabel对于文本颜色需要为灰色的行,我有一组索引。我有一个公共方法InventoryCustomCell,允许我设置UILabels 的颜色。

该公共方法如下所示:

- (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第一次调用。事实并非如此。没有一个单元格会变灰,但将它们变灰的呼唤一直在进行。

如果我使用 default UITableViewCells,我可能只会有一个单独cellIdentifier的黑色/灰色。由于我使用的UITableViewCell是具有笔尖的自定义项,因此我必须注册一个笔尖,因此我cellIdentifier不能在只保留一个XIB文件的情况下使用此方法。

我认为唯一可行的是,如果我创建了一个具有相同单元格视图的新 XIB,但将所有标签添加为灰色。然后我可以使用这两种cellIdentifier方法,但这似乎很老套。

4

1 回答 1

0

只需在自定义单元类的 .h 文件中添加 UILabel 的属性。然后tableView:cellForRowAtIndexPath:做这样的事情:

InventoryCustomCell *cell = (InventoryCustomCell *)[tableView dequeueReusableCellWithIdentifier:inventoryCellID forIndexPath:indexPath];
cell.yourTextLabel.textColor = [UIColor grayColor];

return cell;
于 2013-08-01T20:41:44.040 回答