iOStableView自定义单选cell和多选cell

iOStableView自定义单选cell和多选cell

2023年7月13日发(作者:)

iOStableView⾃定义单选cell和多选cell单选的效果⼤概就是这样,如果想设置右边的accessoryType属性,更改对应的UITableViewCellAccessoryType参数即可。1.先定义⼀个NSIndexPath ⽤于保存最后⼀次选中的组⾏NSIndexPath * _lastSelectIndexPath;的代理⽅法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];if(cell == nil){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];oundColor = [UIColor whiteColor]; = [UIFont systemFontOfSize:12];}if([_lastSelectIndexPath isEqual:indexPath]){//设置选中图⽚ = [UIImage imageNamed:@"icon_ssselected"];}else {//设置未选中图⽚ = [UIImage imageNamed:@"icon_unselected"];} = [NSString stringWithFormat:@"Cell%ld",];return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{UITableViewCell * lastSelectCell = [tableView cellForRowAtIndexPath: _lastSelectIndexPath];if (lastSelectCell != nil) { = [UIImage imageNamed:@"icon_unselected"];}UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; = [UIImage imageNamed:@"icon_ssselected"];_lastSelectIndexPath = indexPath;}多选,其实和单选差不多,只是将⽤于保存最后⼀次选中的NSIndexPath 换成数组就⾏了1.定义⼀个数组⽤于保存选中的NSIndexPath

NSMutableArray * _selectArr;数组记得初始化的代理⽅法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];if(cell == nil){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];oundColor = [UIColor whiteColor]; = [UIFont systemFontOfSize:12];}if([_selectArr containsObject:indexPath]){//设置选中图⽚ = [UIImage imageNamed:@"icon_ssselected"];}else {//设置未选中图⽚ = [UIImage imageNamed:@"icon_unselected"];} = [NSString stringWithFormat:@"Cell%ld",];return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];if([_selectArr containsObject:indexPath]){[_selectArr removeObject:indexPath]; = [UIImage imageNamed:@"icon_unselected"];}else {[_selectArr addObject:indexPath]; = [UIImage imageNamed:@"icon_ssselected"];}}

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689259911a226205.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信