2016-03-08 85 views
0

我有一个自定义UITableViewController,它有一行,只有两个UITableViewCell s。动态更改UITableViewCell文本的textColor

我想动态设置我UITableViewCell的文本基于几件事情的颜色(cell.textLabel.textColor):

1)如果这是第一次启动时,第一个单元格的文本颜色应该是[UIColor whiteColor]和第二小区的文本颜色应该是[UIColor grey1Color]

2)如果用户选择的小区和离开屏幕,并随后返回到表视图,最后选择的单元格的文本颜色应该是[UIColor whiteColor],和的文本颜色没有选择的细胞应该是[UIColor grey1Color]

每当选择一个单元格时,属性都会更新; myCellTextValue。这是为了在这个特定的表视图之外进行一些API调用。

我对实现上述逻辑的想法是使用此属性来确定单元格的文本应该是什么颜色。我下面的代码尝试在cellForRowAtIndexPath

if (cell.textLabel.text == self.myCellTextValue) { 
     cell.textLabel.textColor = [UIColor whiteColor]; 
    } else { 
     cell.textLabel.textColor = [UIColor grey1Color]; 
    } 
} 

然而,这两种细胞的文本颜色始终是灰色的。我敢肯定这主要是因为误解了某些地方的创作。有没有人有任何关于如何正确实施这个指针?谢谢!

编辑:以下@ Gismay的评论,我尝试了下面的代码;但得到了同样的结果:

if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) { 
    cell.textLabel.textColor = [UIColor whiteColor]; 
} else { 
    cell.textLabel.textColor = [UIColor grey1Color]; 
} 

编辑2:我也试过在检查包裹上面的代码,以确保我们只盯着每次一个单元,但这并没有影响之一:

if((indexPath.section==0)&&(indexPath.row==0)){ 
    if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) { 
     cell.textLabel.textColor = [UIColor whiteColor]; 
    } else { 
     cell.textLabel.textColor = [UIColor grey1Color]; 
    } 
} else if((indexPath.section==0)&&(indexPath.row==1)){ 
    if ([cell.textLabel.text isEqualToString:self.myCellTextValue]) { 
     cell.textLabel.textColor = [UIColor whiteColor]; 
    } else { 
     cell.textLabel.textColor = [UIColor grey1Color]; 
    } 
} 
+1

你肯定你的if语句的工作? cell.textLabel.text是一个字符串,所以肯定比较应该是isEqualToString? – Gismay

+0

@Gismay这是一个很好的观点。我刚刚尝试过;看到我上面编辑的 – narner

+0

是self.myCellTextValue是静态的吗? –

回答

1

您可以使用selectedRowIndex作为类级别的变量,并且每次选择某行时都会继续更新。开始 - 无论是选择或不 - 这应该是0,所以,第一行是不同

我想你不想使用的文本价值,因为它可能不是唯一

当你发生什么事多选而不离开视图?大概你需要清除旧行的白色文本,并将其重新设置为新的文本?

最简单的实现方法是重新加载每个选择的tableView - 但如果这需要太长时间,您可以随时重新加载单个行 - 在更新selectedRowIndex之前对行选择设置selectedRowIndexPrevious,并重新加载这两者行。上一行将在灰色重绘,并在新一白

这里是你如何可能实现一些这

class MyViewController : UIViewController 
{ 
    // define the variables to keep track of row selection here 
    var selectedRowIndex : Int = 0 
    var selectedRowIndexPrevious : Int = -1 

    // the rest of your code 

,然后你需要更新selectedRow变量

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     // Uncheck the previous checked row 
     selectedRowIndexPrevious = selectedRowIndex 

     // **UPDATED** need to set the selectedRowIndex 
     selectedRowIndex = indexPath.row 
     // **UPDATED** 

     // reload needs an array of indexPath 
     // so we can supply the previous selection AND the current one 

     NSIndexPath* rowToReloadPrevious = [NSIndexPath indexPathForRow: selectedRowIndexPrevious inSection:0]; 
     NSIndexPath* rowToReloadNew = [NSIndexPath indexPathForRow: selectedRowIndex inSection:0]; 
     NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReloadPrevious, rowToReloadNew, nil]; 
     [tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; 
    } 

内cellForRowAtIndexPath,您只需要查看selectedRowIndex而不是检查文本

if (indexPath.row == selectedRowIndex) { 
    cell.textLabel.textColor = [UIColor whiteColor]; 
} else { 
    cell.textLabel.textColor = [UIColor grey1Color]; 
} 

}

+0

嘿@Russell!回应你关于文本价值不是唯一的观点;那是个很好的观点。那么检查每一行会更好吗? 关于清除旧文本和重绘也是一个好点。 我不确定你使用'selectedRowIndex'作为一个类级变量是什么意思,那会是什么样子? – narner

+1

@narner - 我已经更新了答案。希望这给你你所需要的 – Russell

+0

谢谢,这是超级超级接近。现在虽然会发生什么,但第一个单元总是白色的,第二个单元总是灰色的。也许需要一个'deselectRowForIndexPath'的地方? – narner

1

你可以这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //change your cell text color here 
    cell= [tableView cellForRowAtIndexPath:indexPath] 
    cell.textLabel.textColor = [UIColor whiteColor]; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (cell.isSelected == YES) 
    { 
     cell.textLabel.textColor = [UIColor grey1Color]; 
    } 
    else 
    { 
     cell.textLabel.textColor = [UIColor whiteColor]; 
    } 
} 

另一种方式是继承的tableview细胞并实现以下方法:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
    [self updateTextColor:selected]; 
} 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { 
    [super setHighlighted:highlighted animated:animated]; 
    [self updateTextColor:highlighted]; 
} 

- (void)updateTextColor:(BOOL)isSelected { 
    labelA= //get reference of the cell textlabel 
    if (labelA) { 
     if (isSelected) { 
      [labelA setTextColor:[UIColor whiteColor]]; 
     } else { 
      [labelA setTextColor:[UIColor greyColor]]; 
     } 
    } 


} 
+0

谢谢你,这让我更加接近!唯一不同的是,如果选择,我将需要单元格为白色;而不是灰色 现在发生的是如果我选择一个单元格,它的文本变成白色,然后选择另一个单元格,第一个单元格的文本保持白色而不是灰色。我缺少什么东西? – narner

+0

我更新了答案...请检查它@narner –