ios - 点击单元格时更改表格单元格内 UILabel 的颜色

标签 ios objective-c uitableview uilabel

我有一个包含 UIImage 和 UILabel 的单元格:

enter image description here

我有这段代码来设置它的内容,非常标准的东西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *itemCellID = @"menuItem";

    NSString *currentMenuLabel = [self.menuItemStructure objectAtIndex:[indexPath row]];
    NSString *currentMenuIcon = [self.menuItemIcon objectAtIndex:[indexPath row]]; 
    MTNLeftMenuItemCell *cell = [self.tableView dequeueReusableCellWithIdentifier:itemCellID];
    [cell.leftMenuItemLabel setText:currentMenuLabel];
    UIImage *icon = [UIImage imageNamed:currentMenuIcon];
    [cell.leftMenuItemIcon setImage:icon];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

现在我想做的是在点击单元格时更改此 UILabel 的 文本颜色,有点像 .cell .label:hover { ... } CSS。回想起来这似乎很明显,但让我感到困惑的是 UILabel 作为单元格的 subview 。

我该怎么做?

最佳答案

有一个委托(delegate) didSelectRowAtIndexPath 方法在选择单元格时调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //Get your cell for selected row       
   cell.leftMenuItemLabel.textColor = [UIColor redColor];//Configure whatever color you want
}

关于ios - 点击单元格时更改表格单元格内 UILabel 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604964/

相关文章:

ios - UITableView 开始更新

ios - 来自播放列表项的 YouTube Objective-C API 视频 url

ios - 如何检测触摸输入是否与 uiimageview 相交

iphone - 如何创建一个具有 NavControllers 并在其中保存 tableVC 的 Tabbar?

ios - 如何在表格的开头和结尾锁定表格 View 滚动

ios - 选择行时显示自定义 UIView

ios - UITabBarItem 标签中的多行

ios - 在静态表格 View 中动态调整 TextView 和表格 View 单元格的大小

Objective-C block 语法

ios - 何时更改 iOS 8 的自动单元格高度的单元格约束?