我有这样的菜单:

菜单

每个单元格的正常(未选定)状态是一个图像,选定状态也是一个图像(看起来像默认的蓝色图像).然而,我想添加一个额外的第三个图像,这样当用户 Select 一个单元格时,它会在变成蓝色(选中)之前短暂地改变为第三种 colored颜色 .

这是我的代码:

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setBackgroundColor:[UIColor clearColor]];

    NSString *cellIdentifier = @"MenuItemCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:cellIdentifier];
    }

    UIImage *cellBackgroundNormal = [UIImage imageNamed:@"cell_menu_normal"];
    UIImage *cellBackgroundSelected = [UIImage imageNamed:@"cell_menu_selected"];
    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:cellBackgroundNormal];
    UIImageView *cellBackgroundSelectedView = [[UIImageView alloc] initWithImage:cellBackgroundSelected];
    cell.backgroundView = cellBackgroundView;
    cell.selectedBackgroundView = cellBackgroundSelectedView;

    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell.textLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
    cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];

    return cell;
} 

如你所见,到目前为止,我只有两种状态.我不知道如何才能在第三张图片中引入cell.hoveredBackgroundView个.如果有人能帮我实现这一点,我将不胜感激.

推荐答案

iOS 6.0 and later

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Add your Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor whiteColor] ForCell:cell];  //highlight colour
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Reset Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color

}

- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
    cell.contentView.backgroundColor = color;
    cell.backgroundColor = color;
}

Custom UITableViewCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    UIView * selectedBackgroundView = [[UIView alloc] init];
    [selectedBackgroundView setBackgroundColor:[UIColor colorFromHexString:@"5E6073"]]; // set color here
    [self setSelectedBackgroundView:selectedBackgroundView];
}

Objective-c相关问答推荐

将 NSInteger 转换为 NSIndexpath

Objective-C - 将浮点数转换为字符串的其他方法

将未知数量的行添加到静态单元UITableView

如何将 CGPoint 添加到 NSMutableArray?

Objective-C 中的正式协议和非正式协议有什么区别?

通过 ObjC 类别覆盖方法并调用默认实现?

更改后退导航栏按钮的字体

对块进行类型定义是如何工作的

如何在 iPhone 上画出讲话泡泡?

字符串的静态 NSArray - 如何/在哪里初始化视图控制器

UIKeyboardBoundsUserInfoKey 已弃用,改用什么?

Xcode 在 iOS 8 中的 Main() 中引发异常,并带有所有异常断点

代码问题:格式字符串不是字符串文字

使用单独的委托/数据源时的 UITableView 问题

既然我正在使用 Core Data,如何对我的模型进行单元测试?

为什么 detailTextLabel 不可见?

UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

如何获得设备的比例因子?

重复符号问题

如何在 iOS 中创建自定义 UIActivity?