tableview 点击cell改变cell中的label.text的字体颜色,cell复用出现有关问题的解决方案

tableview 点击cell改变cell中的label.text的字体颜色,cell复用出现问题的解决方案

因为最近偶然要用到,然后先去百度上查找了一下看看是否有相关的Demo或者经验,可能是我查找过于粗略,又或者查找信息不够多,于是没有找到,后经自己下午思考,然后找到了此方法,可能不是最好的,但好在实现了自己想要的结果,下面只是我写的一个小Demo,思路就是这么个思路,如果有更好的方法请在下方留言,不吝赐教。谢。



题目要求:在tableview上实现多选,然后在选中的时候改变其字体颜色,最后将输出结果打印在控制台上。(主要考两点,1:通过tableview的select和Deselect协议实现结果的输出;2:关于cell的复用

主要解决思路:创建两个数组,分别存选中的cell对应的数据模型以及选中的行数然后通过判断行数选择对应的cell样式。


//

//  ViewController.m

//  解决cell选中时字体颜色改变造成复用的Demo

//

//  Created by 程磊 on 14/12/6.

//  Copyright (c) 2014 程磊. All rights reserved.

//


#import "ViewController.h"

#import "TextData.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {

    UITableView *_tableView;

    NSMutableArray *_dataArray;//数据源

    NSMutableArray *_selecteArray;//保存选中的cell对应的模型数据

    NSMutableArray *_indexArray;//保存选中的indexPath

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _dataArray = [[NSMutableArray alloc] init];

    //假设数据源

    for (int i = 0; i < 200; i++) {

        NSString *textString = [NSString stringWithFormat:@"%d",i];

        TextData *data = [[TextData alloc] init];

        data.textInfo = textString;

        [_dataArray addObject:data];

    }

    _selecteArray = [[NSMutableArray alloc] init];

    _indexArray  = [[NSMutableArray alloc] init];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 320, 430) style:UITableViewStylePlain];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    _tableView.allowsSelection = YES;

    _tableView.allowsMultipleSelection = YES;

    [self.view addSubview:_tableView];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame = CGRectMake(0, 20, 320, 30);

    [btn setTitle:@"确定" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}



- (void)btnClick:(UIButton *)btn{

    for (TextData *data in _selecteArray) {

        NSLog(@"%@",data.textInfo);

    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *chenglei = @"chenglei";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:chenglei];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:chenglei];

    }

    //在这里判断,看indexPath是否已经被选中,如果选中就将其对应的那一行的字体颜色设置为选中时的颜色,否则就是默认的颜色

    if ([_indexArray containsObject:indexPath]) {

        cell.textLabel.textColor = [UIColor redColor];

    }else{

        cell.textLabel.textColor = [UIColor blackColor];

    }

    TextData *data = _dataArray[indexPath.row];

    cell.textLabel.text = data.textInfo;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataArray.count;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TextData *data = _dataArray[indexPath.row];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.textLabel.textColor = [UIColor redColor];

    [_selecteArray addObject:data];

    [_indexArray addObject:indexPath];

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    TextData *data = _dataArray[indexPath.row];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.textLabel.textColor = [UIColor blackColor];

    if ([_selecteArray containsObject:data]) {

        [_selecteArray removeObject:data];

    }

    if ([_indexArray containsObject:indexPath]) {

        [_indexArray removeObject:indexPath];

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end