[Objective-C]UITableView使用

UITableView的基本概念

  1. UITableView是用于展示列表信息的非常方便与常用的工具.
  2. UITableView是UISscrollView的子类,因此具有能够滑动的特性,能够上下滑动,同时在cell是可编辑的情况下,能向左滑动

UITableView的基本属性

创建UITableView

UITableView* tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

列表样式

  1. UITableViewStylePlain(常规表视图)UITableViewStylePlain
  2. UITableViewStyleGrouped(分区分组视图)
    UITableViewStyleGrouped
  3. UITableViewStyleInsetGrouped(圆角分区视图)UITableViewStyleInsetGrouped

遵守协议

  1. 使用时应遵守协议<UITableViewDelegate,UITableViewDateSource>
tableView.delegate = self;
tableView.dataSource = self;
  1. 设置分区数量
//设置分区数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
  1. 设置某一分区的行数
//设置某一分区的行数,section返回的是当前分区,根据当前分区设置分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
  1. 设置某一分区某一行的高度
//设置某一分区某一行的高度,indexPath.section是当前分区,indexPath.row是行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
  1. 设置每一行里面的内容

需要注意的是 tableView 必须使用重用机制 , 当cell滑出屏幕的时候 cell会被系统回收,在下面cell将要显示时会调用之前回收的cell,这样就不必每次都创建销毁,节省内存同时节省时间,如果不使用重用机制,每次都创建新的cell 是非常消耗内存和性能的,因此,使用重用是非常非常重要的

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //展示很多数据的时候,避免每次滑动创建新的对象,应判断是否存在再创建对象,节省资源消耗
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.section*2+indexPath.row]];
    return cell;
}
}

成员变量

  1. 设置分割线的长度
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  1. 设置背景颜色
tableView.backgroundColor = [UIColor redColor];

类方法

  1. 设置section头部高度(heightForHeaderInSection)
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
  1. 设置section尾部高度(heightForFooterInSection)
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
  1. 设置section头部视图(viewForHeaderInSection)
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* view = [[UIView alloc]init];
    view.backgroundColor = [UIColor blueColor];
    return view;
}
  1. 设置section尾部视图(viewForFooterInSection)
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView* view = [[UIView alloc]init];
    view.backgroundColor = [UIColor redColor];
    return view;
}
  1. 设置某一行的功能按钮

UITableViewCellEditingStyleNone(无样式)
UITableViewCellEditingStyleDelete(删除按钮)
UITableViewCellEditingStyleInsert(插入按钮)

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        return UITableViewCellEditingStyleInsert;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}
  1. 设置索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G"];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值