UITableViewCell中。 如何在触摸时突出显示单元格?

我有一个自定义tableViewCell。 我想通过突出显示用户触摸。 单元格select样式是UITableViewCellSelectionStyleBlue 。 在父控制器中,我设置了self.clearsSelectionOnViewWillAppear = YES

我应该很好去。 不。 select仍然坚持细胞。 我想要的只是在接触的时间内select指示。 触摸时应立即返回未选定的外观。

我该怎么做呢?

干杯,
道格

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; } 

下面的Swift示例使用didHighlightRowAtIndexPath来更改单元格的背景颜色,并使用didUnhighlightRowAtIndexPath重置颜色 – 请参阅下面的代码:

 // MARK: UITableViewDelegate func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.backgroundColor = UIColor.greenColor() } } func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.backgroundColor = UIColor.blackColor() } } 

覆盖- (void)setSelected:(BOOL)selected animate:(BOOL)animated而不调用super

这工作:

它获取与单元格边框看起来像seperator backgroundview。不要更改默认的表格视图设置在界面builder.Make确保UITableViewCellSelectionStyleNone没有设置为selectionstyle。 我正在粘贴工作代码。 :

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *kCellIdentifier = @"PlayListCell"; UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; } MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row]; cell.textLabel.text = playList.name; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]]; MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ]; cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]]; // the main code which make it highlight UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f]; [bgColorView.layer setBorderColor:[UIColor blackColor].CGColor]; [bgColorView.layer setBorderWidth:1.0f]; [cell setSelectedBackgroundView:bgColorView]; return cell; 

}

我有同样的问题。下面的代码完全为我工作。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor blueColor]]; [table reloadData]; }