【问题标题】:How to change UILabel text color on Custom UITable cell?如何更改自定义 UITable 单元格上的 UILabel 文本颜色?
【发布时间】:2015-07-26 21:34:42
【问题描述】:

我在故事板上创建了一个表格视图控制器。单击所选行时,我想将 UILabel 文本颜色更改为绿色。

我正在尝试这样的事情,但它不起作用:

- (void)viewDidLoad {
    [super viewDidLoad];
    menuItems = @[@"home", @"stamp", @"scanner", @"settings"];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

 NSLog(@"cell  %@",[cell.contentView viewWithTag:1000]);

    return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(indexPath.row == 0){

        UILabel *menu= (UILabel*)[cell.contentView viewWithTag:1000];
        menu.textColor = [UIColor greenColor];
        NSLog(@"cell clicked: %@",[cell.contentView viewWithTag:1000]);

    }
        //[cell.textLabel setTextColor:[UIColor greenColor]];
       // [self setCellColor:[UIColor greenColor] ForCell:cell];
    [self.tableView reloadData];


}

我已经在表格单元格中拖动标签并将标识符设置为 home、stamp、scanner...并将标签更改为 1000。

谁能告诉我为什么标签文本颜色仍然保持不变并为我提供解决方案?

【问题讨论】:

    标签: objective-c uilabel tableviewcell swrevealviewcontroller viewwithtag


    【解决方案1】:

    如果单元格中有多个标签,则需要分别设置其颜色 要获取加载到内存中的单元格,请使用

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    删除if(indexPath.row == 0) 条件以在每个单元格上应用颜色。

    在 cell.contentView 中循环获取标签

    (如果是多个标签,也可以通过标签获取,但对每个标签应用唯一标签,并获取每个标签与标签)

    for (id thisLabel in cell.contentView.subviews) {
        if ([thisLabel isKindOfClass:[UILabel class]]) {
            UILabel *currentlabel = thisLabel;
            [currentlabel setTextColor:[UIColor greenColor]];
        }
    }
    

    对于上面的单标签循环将起作用,但使用标签更容易获得

    UILabel *currentLabel= (UILabel*)[cell.contentView viewWithTag:1000];
    [currentLabel setTextColor:[UIColor greenColor]];
    

    【讨论】:

    • 是的,我有多个单元格。
    • 如果要对所有单元格应用颜色,则适用于多个单元格。按照上述步骤。或者您也可以创建自定义单元格并为每个标签应用颜色。
    • 它工作!谢谢!但是,如果我只想将选定的单元格更改为绿色而其他单元格仍然保持相同的颜色呢?
    • 只需在您的类中创建私有变量作为 NSInteger selectedRow。还将 didSelectRowAtIndexpath 中的 selectedRow 值更新为 selectedRow = indexPath.row。在 cellForRowAtIndexPath 中使用 indexPath.row 检查 selectedRow 并应用与在 didSelectRowAtIndexPath 中相同的颜色。注意:最初 selectedRow 将具有默认值,即 0。因此设置 selectedRow = NSNotFound;以便。它不会在初始加载时将绿色设置为第 0 个索引的标签
    • 你可以自己做。我想我已经解释了解决方案。我知道你是新手,但不要养成使用现成代码的习惯。如果你想从基础学习?从斯坦福 CS193p 下载视频
    【解决方案2】:

    didSelectRowAtIndexPath

     UITableViewCell *cell=(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath.row];
    cell.(whatever your label property).textColor=[UIColor greenColor];
    

    这将改变所选单元格上标签的颜色。

    【讨论】:

    • 什么是标签属性?我无法拖动标签和设置属性。对不起,我只是一个新手>
    • NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.textColor=[UIColor greenColor];
    • 如果单元格上有多个标签?
    • 是的,我有多个单元格
    【解决方案3】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    
        if (selectedRow == 0) {
            UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
            [homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        }else if (selectedRow == 3){
            UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
            [settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
        }
        if(selectedRow!= indexPath.row){
            UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
            [homeLabel setTextColor:[UIColor whiteColor]];
            UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
            [stampLabel setTextColor:[UIColor whiteColor]];
            UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
            [scannerLabel setTextColor:[UIColor whiteColor]];
            UILabel *settingLabel= (UILabel*)[cell.contentView viewWithTag:1400];
            [settingLabel setTextColor:[UIColor whiteColor]];
    
        }
    
        return cell;
    }
    
    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        selectedRow = indexPath.row;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if(indexPath.row == 0){
            UILabel *homeLabel= (UILabel*)[cell.contentView viewWithTag:1000];
            [homeLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
            [self.tableView reloadData];
        }
        if(indexPath.row == 1){
            UILabel *stampLabel= (UILabel*)[cell.contentView viewWithTag:1200];
            [stampLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
            [self.tableView reloadData];
    
        }
        if(indexPath.row == 2){
            UILabel *scannerLabel= (UILabel*)[cell.contentView viewWithTag:1300];
            [scannerLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
            [self.tableView reloadData];
        }
        if(indexPath.row == 3){
            UILabel *settingLabel = (UILabel*)[cell.contentView viewWithTag:1400];
            [settingLabel setTextColor:[UIColor colorWithRed:(163/255.f) green:(217/255.f) blue:(33/255.f) alpha:1.0f]];
            [self.tableView reloadData];
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多