美文网首页
tableview的简单使用

tableview的简单使用

作者: 沙滩上的赤酱 | 来源:发表于2016-09-06 22:01 被阅读0次

UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。

初始化tableview后,要想显示数据,就必须遵守tableview的数据源代理,而且实现以下方法否则程序会崩溃:

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

该方法决定了tableview的每一行显示什么内容,返回值为UITableViewCell,可以通过设置UITableViewCell的各种属性(image,label..)从而达到想要的效果,或者自定义Cell,关于自定义cell在后面会做相关介绍

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

该方法决定了每一组(section)中有几个cell

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

该方法决定了tabview显示几组,这个方法不一定要实现,如果不实现默认显示一组

如果想要实现对tableview 的操作(如:点击每一个cell能获得相应)则必须遵守UITableViewDelegate协议,成为代理

以下是几个比较常用的方法

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

该方法可以设置每个cell 的高度

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;  

该方法可以自定义每个组的头部(图片,按钮等等)

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

该方法可以自定义每个组的尾部(图片,按钮等等)

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

该方法获取当前点击的cell的indexPath(结构体,包括了section和row)

如何复制粘贴剪切一个cell

遵守UITableViewDelegate协议,并实现以下方法

该方法决定在长按cell后 是否显示 复制粘贴 菜单

-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

该方法决定 菜单上显示什么按钮

-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

//    不显示剪切的按钮

if (action==@selector(copy:)||action==@selector(paste:)) {

return YES;

}else{

return NO;

}

}

该方法决定 点击菜单上的复制粘贴按钮 后干啥

-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{

if (action==@selector(copy:)) {

       NSLog(@"copy!");

}else if(action==@selector(paste:)){

       NSLog(@"paste!");

cell 的编辑

设置cell能否被编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

设置删除按钮的文字

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

return @"删除";

}

实现cell 的编辑,通过editingStyle做不同操作

editingStyle:是编辑模式是枚举类型,有以下三种

UITableViewCellEditingStyleNone,

UITableViewCellEditingStyleDelete,

UITableViewCellEditingStyleInsert

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

  }


UITableViewCell的使用

创建TableViewCell

UITableViewCell *cell=[UITableViewCell alloc]initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(nullable NSString *)#>]

reuseIdentifier:重用标识符,定义重用标识符可以实现cell的复用、

TableViewCell的重用:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newscell"];

//复用重用标识符为newscell的cell

if (cell == nil) {

//没有重用标识符为newsreel的cell 创建一个cell并且设置重用标识符

cell=[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsreel"];

}

//注册tableviewcell,这样就不需要对cell进行为空判断

[tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]

//使用xib创建cell,使用下面的方法进行tableviewcell的注册

[tableView registerNib:<#(nullable UINib *)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]

相关文章

网友评论

      本文标题:tableview的简单使用

      本文链接:https://www.haomeiwen.com/subject/kmadettx.html