需求:點按某個Cell的時候cell上面的文字高亮或者改變顏色
在具體的Cell中重寫函數(shù):
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{//重寫高亮函數(shù)
if (highlighted) {
self.textLabel.backgroundColor = [UIColor yellowColor];
self.textLabel.textColor = [UIColor redColor];
self.textLabel.text = [NSString stringWithFormat:@"這是第%ld行 點中我啦哈哈哈",(long)self.tag];
}else{
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.text = [NSString stringWithFormat:@"這是第%ld行 哈哈哈",(long)self.tag];
}
}
還有一種暴露出來的方式,但是并不推薦,以為每次都會調(diào)用tableView cellForRowAtIndexPath:indexPath
方法如下:
在ViewController實現(xiàn)
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor yellowColor];
//cell.contentView.backgroundColor = [UIColor greenColor];
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColor blackColor];
//cell.contentView.backgroundColor = [UIColor grayColor];
}```