2013-06-20 152 views
13

我正在为我的tableview使用自定义表格视图单元格。为了设置边框,我将视图放在了自定义单元格上,并且正在更改其边框属性。更改所选UITableViewCell的边框颜色

self.borderView.layer.borderColor = VIEW_BORDER_COLOR; 

我想通过改变边框的颜色来突出显示所选单元格。我试图改变它在did电子邮件的索引路径,

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

但随着单元格被重新使用它在滚动上发生变化。

+0

你正在做的多个选择或单个选择... –

+0

[细胞setShowsTouchWhenHighlighted:YES];尝试这一个... – Jitendra

回答

19

您可以使用

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

希望它可以帮助你。

4

将必须标记/取消标记(假设ü只需要一次一个选择)边框颜色连连喜欢 -

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

只需保存/缓存选择指数喜欢 -

- (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; 
     } 
+0

很高兴,如果它的工作..可以考虑接受正确的答案:) –

+0

我真的希望我能...没有声誉尚未:( –

+0

borderView不是一个有效的属性UITableViewCell – Necreaux

31

使用:

夫特2

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

夫特3

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