公司最近要弄一个单项选择的列表。想了想就CheckMark比较适合。以前没有怎么接触过,故记此笔记。
因为有多个问题,也就是说有多个section 。所以需要有一个可变数组,记下每次复选项的选中位置。(如果只有一个问题,那么直接使用基本类型即可)。
当选中时做操作:搜索数组内原checkMark属性的是哪个Cell 如果有将其取消。然后讲当前Cell置为CheckMark。再将数组数据更新为当前Cell位置
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
QuestionTVCModel *model=[_dataSource objectAtIndex:indexPath.section];
if (model.checkMark<model.questionAnswer.count) {
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:model.checkMark
inSection:indexPath.section];
UITableViewCell *oldCell=[tableView cellForRowAtIndexPath:oldIndexPath];
oldCell.accessoryType=UITableViewCellAccessoryNone;
}
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
model.checkMark= indexPath.row;
[_dataSource replaceObjectAtIndex:indexPath.section withObject:model];
}
accessoryType 有一个回调方法
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
QuestionTVCModel *model=[_dataSource objectAtIndex:indexPath.section];
if (model.checkMark==indexPath.row) {
return UITableViewCellAccessoryCheckmark;
}else{
return UITableViewCellAccessoryNone;
}
}
复用的时候会用到,即当前Cell如果在数组里为CheckMark ,那么设置。否则设置None.基本到此,可以在此基础上设置字体颜色。或其他。
下贴出ALL代码:
#import "TestViewController.h"
#import "QuestionTVCModel.h"
@interface TestViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataSource;
}
@end
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initiailize];
[self setUI];
}
#pragma mark -初始化
/**初始化数据*/
- (void)initiailize{
_dataSource =[[NSMutableArray alloc]init];
[self httpRequest];
}
- (void)httpRequest{
NSArray *data=@[@{@"questionTitle":@"x",@"questionAnswer":@[@{@"answer":@"没有"},@{@"answer":@"有"}]},@{@"questionTitle":@"x",@"questionAnswer":@[@{@"answer":@"没有"},@{@"answer":@"有"}]},@{@"questionTitle":@"x",@"questionAnswer":@[@{@"answer":@"没有"},@{@"answer":@"有"}]},@{@"questionTitle":@"x",@"questionAnswer":@[@{@"answer":@"没有"},@{@"answer":@"有"}]}];
[self setTableViewData:data];
[HttpRequestManager PostQuestionNairekWithPart:nil block:^(NSDictionary *resultDic) {
if ([[resultDic objectForKey:@"returnState"]integerValue]==0) {
[self setTableViewData:[resultDic objectForKey:@"data"]];
}
}];
}
- (void)setTableViewData:(NSArray *)list{
NSLog(@"list%@",list);
for (NSDictionary *part in list) {
QuestionTVCModel *model=[[QuestionTVCModel alloc]init];
[model setValuesForKeysWithDictionary:part];
model.checkMark=10;
[_dataSource addObject:model];
NSLog(@"model%@",model.questionAnswer);
}
[_tableView reloadData];
}
/**初始化视图*/
- (void)setUI{
_tableView=[[UITableView alloc]init];
_tableView.frame=self.view.bounds;
_tableView.delegate=self;
_tableView.dataSource=self;
[self.view addSubview:_tableView];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"TESTTABLEVIEWCELL"];
}
#pragma mark - tableViewDelegate && dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
QuestionTVCModel *model=[_dataSource objectAtIndex:section];
return model.questionAnswer.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TESTTABLEVIEWCELL"
forIndexPath:indexPath];
QuestionTVCModel *model=[_dataSource objectAtIndex:indexPath.section];
NSDictionary *dict=[model.questionAnswer objectAtIndex:indexPath.row];
cell.textLabel.text=[dict objectForKey:@"answer"];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
QuestionTVCModel *model=[_dataSource objectAtIndex:section];
return [NSString stringWithFormat:@"%@、%@",model.questionId,model.questionTitle];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
QuestionTVCModel *model=[_dataSource objectAtIndex:indexPath.section];
if (model.checkMark<model.questionAnswer.count) {
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:model.checkMark
inSection:indexPath.section];
UITableViewCell *oldCell=[tableView cellForRowAtIndexPath:oldIndexPath];
oldCell.accessoryType=UITableViewCellAccessoryNone;
}
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
model.checkMark= indexPath.row;
[_dataSource replaceObjectAtIndex:indexPath.section withObject:model];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
QuestionTVCModel *model=[_dataSource objectAtIndex:indexPath.section];
if (model.checkMark==indexPath.row) {
return UITableViewCellAccessoryCheckmark;
}else{
return UITableViewCellAccessoryNone;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end