我有一个自定义的tableViewCell。我想通过突出显示来指示用户触摸。小区选择风格是UITableViewCellSelectionStyleBlue
。在父控制器中,我设置了self.clearsSelectionOnViewWillAppear = YES
。UITableViewCell。如何在触摸时突出显示单元格?
我应该很好去。不。选择仍然坚持细胞。我想要的仅仅是触摸时间的选择指示。触摸时应立即返回未选定的外观。
我如何做到这一点?
干杯,
道格
我有一个自定义的tableViewCell。我想通过突出显示来指示用户触摸。小区选择风格是UITableViewCellSelectionStyleBlue
。在父控制器中,我设置了self.clearsSelectionOnViewWillAppear = YES
。UITableViewCell。如何在触摸时突出显示单元格?
我应该很好去。不。选择仍然坚持细胞。我想要的仅仅是触摸时间的选择指示。触摸时应立即返回未选定的外观。
我如何做到这一点?
干杯,
道格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
覆盖- (void)setSelected:(BOOL)selected animate:(BOOL)animated
不调用超
细胞仅选择触摸之后 - 问题想要触摸时改变颜色。 – Zorayr
我有同样的issue.The下面的代码完全为我工作。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor blueColor]];
[table reloadData];
}
这工作:
它得到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;
}
下面夫特示例使用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()
}
}
这是行不通的。颜色不会在触摸时重置 –
您可以在'didUnhighlightRowAtIndexPath:'中放置断点并确认它是在触摸时调用的吗?如果你可以证实这一点,那么下一个罪魁祸首可能是你的变色逻辑。 – Zorayr
对不起,我意识到我的问题是我在故事板中选择了“延迟内容”。 –
我们有一个winna!对于家里的人来说,实际的方法签名是deselectRowAtIndexPath:animated :.甜。干杯。 – dugla
我不敢相信我花了这么多钱来解决这个痛苦问题... 你只是觉得可可触摸会做到这一点didHighlightItemAtIndexPath 任何方式谢谢@Dancreek –