2012-05-01 66 views
15

我有一个自定义的tableViewCell。我想通过突出显示来指示用户触摸。小区选择风格是UITableViewCellSelectionStyleBlue。在父控制器中,我设置了self.clearsSelectionOnViewWillAppear = YESUITableViewCell。如何在触摸时突出显示单元格?

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

我如何做到这一点?

干杯,
道格

回答

42
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
+0

我们有一个winna!对于家里的人来说,实际的方法签名是deselectRowAtIndexPath:animated :.甜。干杯。 – dugla

+0

我不敢相信我花了这么多钱来解决这个痛苦问题... 你只是觉得可可触摸会做到这一点didHighlightItemAtIndexPath 任何方式谢谢@Dancreek –

0

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

+0

细胞仅选择触摸之后 - 问题想要触摸时改变颜色。 – Zorayr

-3

我有同样的issue.The下面的代码完全为我工作。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor blueColor]]; 
[table reloadData]; 
} 
+2

你真的想重新加载每个触摸事件的表吗?如何做主题是绝对错误的。 – surfrider

+0

这会将单元格变成蓝色,但它保持原样并仅在touchUpInside上起作用。只有触摸了吗? –

+0

仅在触摸后才标记单元格 - 问题想要在触摸时更改颜色;另外,我同意@surfrider,为什么要重新加载数据? – Zorayr

0

这工作:

它得到backgroundview与单元格边框看起来像seperator.Do在不改变接口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; 

}

9

下面夫特示例使用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() 
    } 
} 
+0

这是行不通的。颜色不会在触摸时重置 –

+0

您可以在'didUnhighlightRowAtIndexPath:'中放置断点并确认它是在触摸时调用的吗?如果你可以证实这一点,那么下一个罪魁祸首可能是你的变色逻辑。 – Zorayr

+0

对不起,我意识到我的问题是我在故事板中选择了“延迟内容”。 –

相关问题