UITableViewCell. How do I just highlight cell during touch down?

耗尽温柔 提交于 2019-12-12 08:19:09

问题


I have a custom tableViewCell. I want to indicate user touch down by highlighting. The cell selection style is UITableViewCellSelectionStyleBlue. In the parent controller I have set self.clearsSelectionOnViewWillAppear = YES.

I should be good to go. Nope. Selection still sticks to the cell. What I want is selection indication only for the duration of the touch down. The appearance should immediately return to the unselected appearance on touch up.

How do I do this?

Cheers,
Doug


回答1:


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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}



回答2:


Below Swift example uses didHighlightRowAtIndexPath to change the background color of the cell on touch down and didUnhighlightRowAtIndexPath to reset the color - see below:

// 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()
  }
}



回答3:


overwrite - (void)setSelected:(BOOL)selected animate:(BOOL)animated without calling super




回答4:


This works:

It gets the backgroundview with cell border looking like seperator.Do not change the default tableview settings in Interface builder.Make sure UITableViewCellSelectionStyleNone is NOT set to selectionstyle. I am pasting the working code. :

   - (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;

}




回答5:


I had the same issue.The code below perfectly worked for me.

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

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


来源:https://stackoverflow.com/questions/10401949/uitableviewcell-how-do-i-just-highlight-cell-during-touch-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!