UITableView动态存放、重用机制

一、UITableView动态存放

#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *tableview;
    NSMutableArray *_arr;//盛放要显示的东西
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //tableview.rowHeight = 80;//设置行高
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
    //tableview.separatorColor =[UIColor yellowColor];//分割线颜色
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"23.jpg"]];
    tableview.backgroundView = imageview;
    tableview.delegate = self;
    tableview.dataSource = self;//设置数据源
    tableview.backgroundView = imageview;//设置背景图
    [self.view addSubview:tableview];
   
    _arr = [NSMutableArray arrayWithObjects:@"222",@"666",@"555",@"999", nil];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor groupTableViewBackgroundColor];
    button.frame = CGRectMake(300, 20, 60, 60);
    [button setTitle:@"add" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(addcell) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addcell{
   
//    NSArray *arr1 = [NSArray arrayWithObjects:@"王",@"冉", nil];
//    [_arr addObjectsFromArray:arr1];
    [_arr addObject:@"王"];
    [tableview reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//提交修改动作:  再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [_arr removeObjectAtIndex:indexPath.row];
    [tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];//删除行
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableview dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;//设置选中时的背景颜色
   
    UIView *view = [[UIView alloc]initWithFrame:cell.frame];
    view.backgroundColor = [UIColor greenColor];
    cell.selectedBackgroundView = view;
    cell.textLabel.text = _arr[indexPath.row];
    cell.textLabel.highlightedTextColor = [UIColor brownColor];//选中状态下,字体颜色
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//自定义右侧视图
    return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"取消选中");
    //NSLog(@"%zi",indexPath.row);
}
//cell被选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableview deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
}

二、重用机制

重用机制:当我们使用tableView时,系统只会创建屏幕中显示的cell个数+1,当cell滑出可视范围时,会将此Cell放入重用池,当有新的cell滑进可视范围时,会先到重用池找有没有可以使用的cell(根据标识),如果能找到,则拿出来直接用,否则再创建新的cell
 
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.dataSource = self;
    [self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
   // NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识,以解决重用机制的产生的bug
 
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
 
    cell.textLabel.text = nil;//先清理,在使用
   
    if (indexPath.row == 1) {
        cell.textLabel.text = @"1212";
    }
    return cell;
}
原文地址:https://www.cnblogs.com/wxzboke/p/4978419.html