【问题标题】:How to get the UITAbleViewCell highlighted state to change the cell background?如何获取 UITableViewCell 突出显示状态以更改单元格背景?
【发布时间】:2013-06-05 04:26:00
【问题描述】:

当用户点击单元格(突出显示状态)时,我正在尝试更改单元格背景图像,我一直在尝试这种方式,但它不起作用:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentRight;
    cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
    cell.textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x63.png"] highlightedImage:[UIImage imageNamed:@"row320x63_pressed.png"]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x63.png"]];
    cell.textLabel.text = [self.listOfMenuSettings objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"settings_icon_%d", indexPath.row]];

    UIImageView *pressed = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x63_pressed.png"]];
    [cell setSelectedBackgroundView:pressed];

    return cell;
}

我错过了什么?

【问题讨论】:

  • 图片名称正确吗?
  • 我尝试了一个示例来检查这一点,我只需要在 cellForRowAtIndexPath: 中设置 backgroundView 和 selectedBackgroundView 。没有其他的。它开箱即用。

标签: ios uitableview background highlight


【解决方案1】:

我在苹果文档中找不到setSelectedBackgroundView: 实例方法。但是有一个属性selectedBackgroundView

所以试试:

cell.selectedBackgroundView = pressed;

【讨论】:

  • selectedBackgroundView 是一个属性所以可以两种方式设置,没有区别。
  • @IdanMoshe 选择单元格时,通过调试此方法中单元格的背景属性设置来检查:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ }
  • 我在 didSelectRowAtIndexPath 中做事,所以我知道 didSelectRowAtIndexPath 没有搞砸。
  • @IdanMoshe 我建议检查一下,以便您知道背景是 nil 还是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2013-01-09
相关资源
最近更新 更多