【问题标题】:How to change custom cell image and label font color when selected in uitableview?在uitableview中选择时如何更改自定义单元格图像和标签字体颜色?
【发布时间】:2013-02-10 09:29:15
【问题描述】:

我有一个带有图像视图和标签的自定义单元格。当用户在表格视图中选择特定单元格时,我想更改其图像和标签字体颜色。我该怎么做?我想避免默认的单元格选择方式(以蓝色突出显示单元格)并使用上述方法代替。

【问题讨论】:

    标签: iphone xcode uitableview uiimageview


    【解决方案1】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
         MyCustomCell *selectedCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    
         //selectedCell.textLabel.textColor = <#your color#>
         //selectedCell.myImage = <#your image>
    
    }
    

    如果你想改变选定的单元格背景颜色,试试这个。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         NSString *cellId = @"cellIdentifier";
    
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
         if(!cell)
         {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    
            UIView *bgColorView = [[UIView alloc] init];
            [bgColorView setBackgroundColor:[UIColor redColor]];
            [cell setSelectedBackgroundView:bgColorView];
         }
    }
    

    【讨论】:

    • 谢谢。我已经这样做了。但是当我选择另一个单元格时,第一个选定的单元格和第二个选定的单元格都有突出显示的图像。我想避免这种情况。我想要默认单元格选择行为。(当我在选择第一个单元格后选择另一个单元格时,第一个单元格图像应该是正常图像,第二个单元格图像应该是突出显示的图像)我该如何实现?
    • 我通过使用 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {//selectedCell.textLabel.textColor = //selectedCell 完成了它.myImage = }然后我可以在取消选择单元格时恢复正常图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2015-02-13
    相关资源
    最近更新 更多