要做的需求如下图,当选到最后一行时,对选中的cell设置文字变为蓝色,右边出现☑️图标
这里介绍不需要自定义cell,相当简洁的办法,直接上代码
在cellForRowAtIndexPath方法里设置
//设置selectedBackgroundView
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
//创建好选中状态需要显示的图标
CGFloat imageViewWidth = 20;
CGFloat imageViewheight = 20;
UIImageView *selectedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
selectedImageView.image = [UIImage imageNamed:@"hp_sortIcon_selected"];
selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.selectedBackgroundView addSubview:selectedImageView];
//textLabel的选中状态
cell.textLabel.highlightedTextColor = [UIColor colorMain];
另外需要默认指定第一行时,可在添加
if (indexPath.row==0) {//指定第一行为选中状态
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
这样就ok了,不需要自定义cell,完全不用考虑设置当前选中cell的select状态和取消其他cell的select状态