当前位置: 移动技术网 > 移动技术>移动开发>IOS > ios设置Cell的默认选中第一行

ios设置Cell的默认选中第一行

2018年11月03日  | 移动技术网移动技术  | 我要评论

ios设置cell的默认选中第一行,要做的需求如下图,当选到最后一行时,对选中的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状态。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网