Подвид UITableViewCell исчезает при выделении ячейки - PullRequest
173 голосов
/ 19 июля 2011

Я реализую табличное представление с выбором цвета, где пользователь может выбрать, скажем, 10 цветов (зависит от продукта). Пользователь также может выбрать другие параметры (например, емкость жесткого диска, ...).

Все параметры цвета находятся в собственном разделе таблицы.

Я хочу отобразить небольшой квадрат слева от textLabel, показывающий фактический цвет.

Прямо сейчас я добавляю простой квадрат UIView, присваиваю ему правильный цвет фона, например:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RMProductAttributesCellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:RMProductAttributesCellID] autorelease];
        cell.indentationWidth = 44 - 8;

        UIView *colorThumb = [[[UIView alloc] initWithFrame:CGRectMake(8, 8, 28, 28)] autorelease];
        colorThumb.tag = RMProductAttributesCellColorThumbTag;
        colorThumb.hidden = YES;
        [cell.contentView addSubview:colorThumb];
    }

    RMProductAttribute *attr = (RMProductAttribute *)[_product.attributes objectAtIndex:indexPath.section];
    RMProductAttributeValue *value = (RMProductAttributeValue *)[attr.values objectAtIndex:indexPath.row];
    cell.textLabel.text = value.name;
    cell.textLabel.backgroundColor = [UIColor clearColor];

    UIView *colorThumb = [cell viewWithTag:RMProductAttributesCellColorThumbTag];
    colorThumb.hidden = !attr.isColor;
    cell.indentationLevel = (attr.isColor ? 1 : 0);

    if (attr.isColor) {
        colorThumb.layer.cornerRadius = 6.0;
        colorThumb.backgroundColor = value.color;
    }

    [self updateCell:cell atIndexPath:indexPath];

    return cell;
}

Прекрасно отображается без проблем.

Моя единственная проблема заключается в том, что когда я выбираю «цветную» строку во время анимации выделения с переходом в синий цвет, мой пользовательский UIView (colorThumb) скрывается. Он снова становится видимым сразу после окончания анимации выбора / отмены выбора, но это приводит к появлению уродливого артефакта.

Что я должен сделать, чтобы исправить это? Разве я не вставляю подпредставление в нужном месте?

(Ничего особенного в didSelectRowAtIndexPath нет, я просто изменяю аксессуар ячейки на флажок или ничего и отменяю выбор текущего indexPath).

Ответы [ 21 ]

1 голос
/ 31 марта 2016

Нарисуйте вид вместо установки цвета фона

import UIKit

class CustomView: UIView {

    var fillColor:UIColor!

    convenience init(fillColor:UIColor!) {
        self.init()
        self.fillColor = fillColor
    }

    override func drawRect(rect: CGRect) {
        if let fillColor = fillColor {
            let context = UIGraphicsGetCurrentContext()
            CGContextSetFillColorWithColor(context, fillColor.CGColor);
            CGContextFillRect (context, self.bounds);

        }
    }


}
0 голосов
/ 09 марта 2018

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

Решением, которое я придумал, было создание подкласса UIView, поэтому он игнорирует обычную настройку цвета фона и добавляет отдельную функцию для обхода защиты.

Swift 4

class MyLockableColorView: UIView {
    func backgroundColorOverride(_ color: UIColor?) {
            super.backgroundColor = color
    }

    override var backgroundColor: UIColor? {
        set {
            return
        }
        get {
            return super.backgroundColor
        }
    }
}
0 голосов
/ 23 марта 2017

Если указанное выше фоновое решение не решает вашу проблему, ваша проблема может лежать в вашем datasource для вашего tableView.

Для меня я создал экземпляр объекта DataSource (называемый BoxDataSource) для обработки методов tableView делегата и dataSource, например:

//In cellForRowAtIndexPath, when setting up cell
let dataSource = BoxDataSource(delegate: self)
cell.tableView.dataSource = dataSource
return cell

Это вызывало освобождение источника данных при каждом касании ячейки, и, таким образом, все содержимое исчезало. Причина в том, что ARC освобождает / собирает мусор.

Чтобы это исправить, мне нужно было зайти в пользовательскую ячейку и добавить переменную источника данных:

//CustomCell.swift
var dataSource: BoxDataSource?

Затем вам нужно установить dataSource на источник данных ячейки var, который вы только что создали в cellForRow, так что это не связано с ARC.

cell.statusDataSource = BoxAssigneeStatusDataSource(delegate: self)
cell.detailsTableView.dataSource = cell.statusDataSource
return cell

Надеюсь, это поможет.

0 голосов
/ 07 декабря 2016

Добавление другого решения, если вы используете раскадровки.Создайте подкласс UIView, который не позволяет устанавливать backgroundColor после его первоначальной установки.

@interface ConstBackgroundColorView : UIView

@end

@implementation ConstBackgroundColorView

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (nil == self.backgroundColor) {
        [super setBackgroundColor:backgroundColor];
    }
}

@end
0 голосов
/ 07 декабря 2016

Это похоже на ответ Павла Гурова, но более гибко в том смысле, что позволяет любому цвету быть постоянным.

class PermanentBackgroundColorView: UIView {
    var permanentBackgroundColor: UIColor? {
        didSet {
            backgroundColor = permanentBackgroundColor
        }
    }

    override var backgroundColor: UIColor? {
        didSet {
            if backgroundColor != permanentBackgroundColor {
                backgroundColor = permanentBackgroundColor
            }
        }
    }
}
0 голосов
/ 18 октября 2016

Поместите этот код в ваш подкласс UITableViewCell

Синтаксис Swift 3

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    if(selected) {
        lockerSmall.backgroundColor = UIColor.init(red: 233/255, green: 106/255, blue: 49/255, alpha: 1.0)
    }
}


override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)

    if(highlighted) {
        lockerSmall.backgroundColor = UIColor.init(red: 233/255, green: 106/255, blue: 49/255, alpha: 1.0)
    }
}
0 голосов
/ 14 октября 2016

Не забудьте переопределить setSelected, а также setHighlighted

override func setHighlighted(highlighted: Bool, animated: Bool) {

    super.setHighlighted(highlighted, animated: animated)
    someView.backgroundColor = .myColour()
}

override func setSelected(selected: Bool, animated: Bool) {

    super.setSelected(selected, animated: animated)
    someView.backgroundColor = .myColour()
}
0 голосов
/ 08 сентября 2016

Основываясь на ответе Павла Гурова, я реализовал в Xamarin.iOS следующее. Версия C #:

NeverClearView.cs

public partial class NeverClearView : UIView
{
    public NeverClearView(IntPtr handle) : base(handle)
    {
    }

    public override UIColor BackgroundColor
    {
        get
        {
            return base.BackgroundColor;
        }
        set
        {
            if (value.CGColor.Alpha == 0) return;

            base.BackgroundColor = value;
        }
    }
}

NeverClearView.designer.cs

[Register("NeverClearView")]
partial class NeverClearView
{
    void ReleaseDesignerOutlets()
    {
    }
}
0 голосов
/ 14 января 2015

вот мое решение, используйте contentView, чтобы показать selectionColor, оно отлично работает

#import "BaseCell.h"

@interface BaseCell ()
@property (nonatomic, strong) UIColor *color_normal;
@property (nonatomic, assign) BOOL needShowSelection;
@end


@implementation BaseCell
@synthesize color_customSelection;
@synthesize color_normal;
@synthesize needShowSelection;

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setup];
}

- (void)setup
{
    //save normal contentView.backgroundColor
    self.color_normal = self.backgroundColor;
    if (self.color_normal == nil) {
        self.color_normal = [UIColor colorWithRGBHex:0xfafafa];
    }
    self.color_customSelection = [UIColor colorWithRGBHex:0xF1F1F1];
    self.accessoryView.backgroundColor = [UIColor clearColor];
    if (self.selectionStyle == UITableViewCellSelectionStyleNone) {
        needShowSelection = NO;
    }
    else {
        //cancel the default selection
        needShowSelection = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}

/*
 solution is here
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if (needShowSelection) {
        self.contentView.backgroundColor = self.backgroundColor = color_customSelection;
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    if (needShowSelection) {
        self.contentView.backgroundColor = self.backgroundColor = color_normal;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (needShowSelection) {
        UIColor *color  = selected ? color_customSelection:color_normal;
        self.contentView.backgroundColor = self.backgroundColor = color;
    }
}
0 голосов
/ 14 марта 2019

SIMPLEST решение без ошибок с анимацией (как в ответе с самым высоким рейтингом) и без подклассов и рисования - установите цвет границы слоя вместо backgroundColor и установите очень большую ширину границы.

colorThumb.layer.cornerRadius = 6
colorThumb.layer.borderWidth = colorThumb.frame.width
colorThumb.layer.borderColor = value.color
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...