iOS中tableview实现单选和多选

本文介绍了如何在iOS应用中使用TableView实现单选和多选功能。通过调整accessoryType属性,无需在Model中维护选中状态,即可轻松达成目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单选:

@interface dataModel : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign,getter=isSelected) BOOL selected;
@end

@implementation dataModel
@end
@interface customTableviewCell : UITableViewCell
@property (nonatomic, strong) UIButton *customAccess;
@end
@implementation customTableviewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        _customAccess = [[UIButton alloc] init];
        _customAccess.contentMode = UIViewContentModeCenter;
        _customAccess.userInteractionEnabled = NO;
        
        [_customAccess setBackgroundImage:[UIImage imageNamed:@"no_selected"] forState:UIControlStateNormal];
        [_customAccess setBackgroundImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
        [_customAccess sizeToFit];
        self.accessoryView = _customAccess;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return self;
}

@end
@interface ViewController ()<UITableViewDataSource,
                                UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *datasArray;

//最后一次选中的行
@property (nonatomic, assign)NSInteger lastIndexRow;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableview];

}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
        for (int i = 0; i < 100; i ++) {
            dataModel *model = [[dataModel alloc] init];
            model.title = [NSString stringWithFormat:@"%d",i];
            if (i == 0)
            {
                model.selected = YES;
                _lastIndexRow = i;
            }
            [_datasArray addObject:model];
        }
    }
    return _datasArray;
}
- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height) style:UITableViewStyleGrouped];
        _tableview.dataSource = self;
        _tableview.delegate = self;
    }
    
    return _tableview;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedCellId = @"CellId";
    customTableviewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:reusedCellId];
    if (!cell)
    {
        cell = [[customTableviewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reusedCellId];
    }
    dataModel *model = self.datasArray[indexPath.row];
    cell.textLabel.text = model.title;
    cell.customAccess.selected = model.isSelected;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSInteger newRow = indexPath.row;
    dataModel *model = self.datasArray[newRow];
    if (model.isSelected)
    {
        //已经选中了。就直接返回。
        return;
    }
    //是新的选中行。取消上一次的选中行
    //更新老的数据源
    dataModel *oldModel = self.datasArray[_lastIndexRow];
    oldModel.selected = !oldModel.isSelected;
    NSIndexPath * indexP = [NSIndexPath indexPathForRow:_lastIndexRow inSection:0];
    customTableviewCell *oldCell = [self.tableview cellForRowAtIndexPath:indexP];
    oldCell.customAccess.selected = oldModel.selected;
    //更新新的选中行
    model.selected = !model.isSelected;
    _lastIndexRow = newRow;
    customTableviewCell *newCell = [tableView cellForRowAtIndexPath:
                                          indexPath];
    
    newCell.customAccess.selected = model.isSelected;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20.0;
}

当用系统的accessoryType的时候。可以不需要model中的选中状态。直接用下面的代码替换。道理类似。

if (newCell.accessoryType == UITableViewCellAccessoryCheckmark)
    {//当已经是选中状态时候直接返回。 
        return; 
    }
多选:更简单。不需要lastselectindex属性。因为不需要改名oldcell的状态。直接把newcell的状态取反即可。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值