iOS beginUpdates && endUpdates用法

来源:互联网 发布:qt连接oracle数据库 编辑:程序博客网 时间:2024/06/05 17:47

(一)

http://blog.sina.com.cn/s/blog_7b9d64af0101b82p.html

http://blog.csdn.net/lotheve/article/details/50993817

http://blog.csdn.net/vieri_ch/article/details/46893023

如果只是单纯的一个插入或者删除操作,没必要用beginUpdatesendUpdates包裹操作方法;若是批量操作,建议用endUpdatesendUpdates,以保证界面对各个更新操作同时响应。


(二)

 让我们来重点关注这行代码:[tableView beginUpdates];

文档中对这行代码的解释为让TableView产生插入,删除或重新加载cell

看到这里大家应该就恍然大悟了吧?原来当我们点击了一个cell后我们相当于重新加载了一遍我们的tableview,但是却和[tableView reloadata]是完全不一样的风格,reloadData这个方法会让tableView整体重新加载,相当于是作用在tableView上,而beginUpdates只是作用在cell上!


上代码

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSArray *datas;

    NSIndexPath *inp;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    UITableView *tbv = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

    [self.viewaddSubview:tbv];

    

    tbv.dataSource =self;

    tbv.delegate =self;

    

    datas = [[NSArrayalloc] initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil];

}


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

    

    returndatas.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if(inp) {

        if (inp.section == indexPath.section && inp.row == indexPath.row) {

            

            return60 * 2.0;

            inp =nil;

        }

    }

    

    

    return60;

}

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

    

    staticNSString *identifier = @"identifier";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

    }

    

    cell.textLabel.text = [datasobjectAtIndex:indexPath.row];

    return cell;

}


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


    [tableView deselectRowAtIndexPath:indexPathanimated:TRUE];

    

    //获取当前indexPath并判断对应的Cell是否被选中


    inp = indexPath;

    

    //最神奇的地方!!

    [tableView beginUpdates];

    [tableView endUpdates];

}



0 0
原创粉丝点击