【问题标题】:HIghlight UITableViewCell with image用图像高亮 UITableViewCell
【发布时间】:2014-06-04 21:31:13
【问题描述】:

在我的应用程序中,我使用的是 UITableView。我想用图像突出显示单元格。 我的代码是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [slideMenuTableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selected_tab_bg.png"]];
cell.selectedBackgroundView = image;
}

它突出显示单元格,但是当我选择另一个单元格时,前一个单元格也被选中,如下所示。当我选择另一个单元格时,我想清除上一个单元格

在屏幕截图中,列表视图和搜索被突出显示。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    like didSelectRowAtIndexPath 委托方法:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [slideMenuTableView cellForRowAtIndexPath:indexPath];
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selected_tab_bg.png"]];
        cell.selectedBackgroundView = image;
        }
    

    还有一个名为didDeselectRowAtIndexPath的委托方法

    在那边写下这段代码:

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.selectedBackgroundView = nil; // or set anything else like normal cell
    }
    

    希望这是您正在寻找的。​​p>

    【讨论】:

      【解决方案2】:

      cellForRowAtImagePath 可能会向您发送一个回收的单元格,它可能是也可能不是您之前突出显示的单元格。如果您已将一个单元格设置为突出显示,那么您需要记住该单元格并取消设置它。或者,您可以在 cellForRowAtImagePath 中设置一个 if 条件,并确保它取消设置先前突出显示并根据当前 indexPath 设置当前突出显示,然后在 tableview 上调用 reloadData 以使其重绘所有内容。

      【讨论】:

      • 是的,我做到了。谢谢欧文
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2012-04-29
      • 2012-10-17
      • 2016-08-05
      相关资源
      最近更新 更多