Подвид UITableViewcell исчезает, когда UItableViewcell выделен, как это исправить?

Я работаю с RESIDEMENU и пытаюсь добавить строку между ячейками LeftMenuViewController, вот мой код:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
    cell.selectedBackgroundView = [[UIView alloc] init];
}

NSArray *titles = @[@"Home", @"Calendar", @"Profile", @"Settings", @"Log Out"];
NSArray *images = @[@"IconHome", @"IconCalendar", @"IconProfile", @"IconSettings", @"IconEmpty"];
cell.textLabel.text = titles[indexPath.row];
cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
UIView * lineView= [[UIView alloc]initWithFrame:CGRectMake(10, 0, cell.contentView.bounds.size.width, 3)];
lineView.backgroundColor=[UIColor redColor];

[cell.contentView addSubview:lineView];

return cell;
}

Сначала я вижу эти линии, но когда я касаюсь любой ячейки, ячейка выделяется, и линия этой ячейки странно исчезает. Как это исправить?

снимок до: введите здесь описание изображения

снимок при нажатии на ячейку: введите здесь описание изображения


person wj2061    schedule 20.07.2015    source источник
comment
можешь поделиться скринами?   -  person Teja Nandamuri    schedule 20.07.2015
comment
Я нашел ответ: stackoverflow.com/q/6745919/4975761   -  person wj2061    schedule 20.07.2015


Ответы (2)


UITableViewCell изменяет цвет фона всех дополнительных представлений, когда ячейка выбрана или выделена.

У вас есть три варианта:

  1. Нет стиля выбора

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  2. Подкласс UITableViewCell и переопределение ячеек Tableview setSelected:animated и/или setHighlighted:animated

  3. Добавьте линию как слой

    CALayer* layer = [CALayer layer];
    layer.frame = CGRectMake(10, 0, cell.contentView.bounds.size.width, 3);
    layer.backgroundColor = [UIColor redColor].CGColor;
    [cell.contentView.layer  addSublayer:layer];
    
person Alaeddine    schedule 20.07.2015
comment
Ни cell.backgroundView, ни cell.selectedBackgroundView не работают. - person wj2061; 20.07.2015
comment
@JunWang Я обновил ответ, у меня есть тесты на третье решение, и оно отлично работает - person Alaeddine; 20.07.2015
comment
Большое спасибо, это самый простой способ решить эту проблему. - person wj2061; 20.07.2015

Другой обходной путь — перезаписать setBackgroundColor: и предоставить аналогичный метод для изменения его backgroundColor.

@interface

- (void)setPersistentBackgroundColor:(UIColor*)color;

@implementation

- (void)setPersistentBackgroundColor:(UIColor*)color
{
    super.backgroundColor = color;
}

- (void)setBackgroundColor:(UIColor *)color
{
    // [super setBackgroundColor:color];
    // prohibit from changing its background color.
}
person tounaobun    schedule 23.11.2015