Подтвердить что ты не робот

Изменить цвет границы UITableViewCell на выбор

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

self.borderView.layer.borderColor = VIEW_BORDER_COLOR;

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

cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;

но при повторном использовании ячеек он изменяется при прокрутке.

4b9b3361

Ответ 1

Использование:

Swift 2:

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.grayColor().CGColor

Swift 3

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor

Ответ 2

Вы можете использовать

[cell.contentView.layer setBorderColor:[UIColor blackColor].CGColor]; 
[cell.contentView.layer setBorderWidth:2.0f]; 

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

Ответ 3

Пометить/отменить отметку (при условии, что вам нужно только одно выбранное за раз) цвет рамки снова и снова, как -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==self.selectedRow){
cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;
}else {
cell.borderView.layer.borderColor = [UIColor clearColor].CGColor;
}
}

Просто сохраните/кешируйте выбранный индекс like-

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

//Unselected the prevoius selected Cell
            YourCellClass *aPreviousSelectedCell=  (YourCellClass*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedRow inSection:0]];
            aPreviousSelectedCell.borderView.layer.borderColor = [UIColor clearColor].CGColor;

//Selected the new one-    
           YourCellClass *aSelectedCell = (YourCellClass*)[tableView cellForRowAtIndexPath:indexPath];

    aSelectedCell.borderView.layer.borderColor = [UIColor yellowColor].CGColor; 

            self.selectedRow = indexPath.row;
        }