关于Cell的复用问题,上次已经说了一种,但似乎那种方法不是最好的,所以说,今天下午根据别人提示,想到了此方法。还是老样子,可能不是最好的,但是实现了功能,至少比上次的要好一些。
题目要求:定义固定数据源,然后让tableview的行上各自显示第几行,然后点击选中的时候,字体颜色会变为红色,取消选中的时候字体变为黑色。然后最后的时候要输出选中的结果
解题思路:首先实现tableView的几个协议,然后定义一个模型,在模型中定义一个标识,然后通过点中的时候标识,然后判断标识解决Cell的复用。
Model中
//
// InfoData.h
// cell复用2Demo
//
// Created by 程磊 on 14/12/8.
// Copyright (c) 2014年 程磊. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface InfoData :NSObject
@property (nonatomic,strong) NSString *labelInfo;
@property (nonatomic,assign) int b;
@end
//
// ViewController.m
// cell复用2Demo
//
// Created by 程磊 on 14/12/8.
// Copyright (c) 2014年 程磊. All rights reserved.
//
#import "ViewController.h"
#import "InfoData.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate> {
UITableView *_tableView;
NSMutableArray *_dataArray;
NSMutableArray *_selectedArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dataArray = [[NSMutableArray alloc] init];
_selectedArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++) {
NSString *str = [NSString stringWithFormat:@"第%d行",i];
InfoData *data = [[InfoData alloc] init];
data.labelInfo = str;
data.yesOrNo =10;
[_dataArrayaddObject:data];
}
_tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,50, 320, 430)style:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
_tableView.allowsSelection =YES;
_tableView.allowsMultipleSelection =YES;
[self.viewaddSubview:_tableView];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn.frame =CGRectMake(0,20, 320, 30);
[btn setTitle:@"确定"forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
- (void)btnClick:(UIButton *)btn{
for (InfoData *datain _selectedArray) {
NSString *str = [NSString stringWithString:data.labelInfo];
NSLog(@"%@",str);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"ID";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
}
InfoData *data = _dataArray[indexPath.row];
if (data.yesOrNo == 100) {
cell.textLabel.textColor = [UIColorredColor];
}else{
cell.textLabel.textColor = [UIColorblackColor];
}
cell.textLabel.text = data.labelInfo;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return_dataArray.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
InfoData *data = _dataArray[indexPath.row];
data.yesOrNo =100;
[_selectedArrayaddObject:data];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColorredColor];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
InfoData *data = _dataArray[indexPath.row];
if ([_selectedArray containsObject:data]) {
[_selectedArrayremoveObject:data];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.textColor = [UIColorblackColor];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end