2013-08-01 44 views
1

我有这个来了几次,每次我从来没有找到一个“优雅”的解决方案。自定义更改的UITableViewCell文本颜色

问题: 我有一个客户UITableViewCellXIBInventoryCustomCell。该单元格有UILabelsUILabel的默认文字颜色为黑色。我有一行索引数组,其中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的方法,但这只是看起来很诡异。

+0

这些单元格在黑色文本表中显示吗? – Justin

+0

是的,他们确实显示为黑色 – random

+0

如果您不执行registerNib方法会发生什么?如果你使用'会发生什么[一个NSBundle mainBundle] loadNibNamed:@“InventoryCustomCell]' – Justin

回答

0

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

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

return cell; 
+0

这不起作用,比你的帮助 – random

+0

我注意到你在注册你的笔尖时使用了字符串,而在dequeueReusableCellWithIdentifier中使用了字符串变量。你确定这些字符串值是相同的吗? – hgwhittle

+0

它们在两个地方都是一样的 – random

相关问题