1.创建tableview:注意这时候我们应该首先把cell放入队列,目的就是为了在使用的时候直接从队列里面取,而不是再去创建,我们这里用三种类型的cell做一个列子
-(void)uiConfig{
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.scrollEnabled = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[MyCellOne class]
forCellReuseIdentifier:KCellone];
[self.tableView registerClass:[MyCellTwo class]
forCellReuseIdentifier:KCelltwo];
[self.tableView registerClass:[MyCellThree class] forCellReuseIdentifier:KCellthree];
[self.view addSubview:self.tableView];
}
2.需要创建一个数据模型给一个属性用来作为判断复用什么类型的cell
-(void)prepareData{
self.detailArray = @[KCellone,KCelltwo,KCellthree];
for (int i = 0; i< 10; i++) {
TestModel *model = [[TestModel alloc]init];
if (i%2==0) {
model.type = 1;
}else{
model.type = 2;
}
model.name = [NSString stringWithFormat:@"李%d",i];
model.phone = [NSString stringWithFormat:@"123344%d",arc4random()%100];
[self.dataArray addObject:model];
}
for (int i = 0; i<10; i++) {
TestModel *model = [[TestModel alloc]init];
if (i%2==0) {
model.type = 1;
}else{
model.type = 2;
}
model.name = [NSString stringWithFormat:@"王八旦%d",i];
model.phone = [NSString stringWithFormat:@"7895541%d",arc4random()%200];
[self.dataArray addObject:model];
}
for (int i = 0; i<20; i++) {
TestModel *model = [[TestModel alloc]init];
model.type = 3;
model.name = [NSString stringWithFormat:@"王八旦%d",i];
model.phone = [NSString stringWithFormat:@"7895541%d",arc4random()%200];
[self.dataArray addObject:model];
}
}
3.cell的复用情况以及如何取就不说了,能看到这个的应该都懂
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentify ;
TestModel *model = _dataArray[indexPath.row];
if (model.type == 1) {
cellIdentify = KCellone;
}else if(model.type == 2){
cellIdentify = KCelltwo;
}else if(model.type == 3){
cellIdentify = KCellthree;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([cellIdentify isEqualToString:@"KCellWeather"]) {
}
else if([cellIdentify isEqualToString:@"KCellAccross"]){
}else if([cellIdentify isEqualToString:@"KCellthree"]){
}
return cell;
}
4.关于tableview的复用问题有很多,总之使用的时候多加注意就好参看demon点击打开链接