单选:
@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的状态取反即可。