TableViewCell 的CheckMark属性

公司最近要弄一个单项选择的列表。想了想就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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值