UITableViewCell. Как просто выделить ячейку во время касания?

У меня есть пользовательский tableViewCell. Я хочу указать, что пользователь прикоснулся, выделив. Стиль выбора ячейки UITableViewCellSelectionStyleBlue. В родительском контроллере я установил self.clearsSelectionOnViewWillAppear = YES.

Я должен хорошо поехать. Неа. Отбор все еще прилипает к ячейке. То, что я хочу, - это индикация выбора только на время касания. Внешний вид должен немедленно вернуться к невыбранному виду при касании.

Как это сделать?

Cheers,
Дуг

Ответы

Ответ 1

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Ответ 2

В примере ниже Swift используется didHighlightRowAtIndexPath для изменения цвета фона ячейки при касании и didUnhighlightRowAtIndexPath до reset цвета - см. ниже:

// 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

перезаписать - (void)setSelected:(BOOL)selected animate:(BOOL)animated без вызова super

Ответ 4

Это работает:

Он получает backgroundview с границей ячейки, похожей на seperator. Не изменяйте настройки tableview по умолчанию в построителе интерфейса. Убедитесь, что 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;

}

Ответ 5

У меня была такая же проблема. Код ниже отлично работал у меня.

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

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