UITableView 详解

2021-02-03  本文已影响0人  文子飞_

UITableView 作为 iOS 中最常见的控件 ,在APP中出现频率颇高,作为第一篇文章,就索性将其整理一下


目录

1. UITableView的基本概念
2. UITableView的最基本属性
3. UITableViewCell的基本属性
!!. UITableView要分组,创建时一定要指定Grouped样式。initWithFrame:style:UITableViewStyleGrouped。否则reloadData出现界面数据显示缺少或其他异常

UITableView 的基本概念

UITableView 的基本属性

需要注意的是 分区头和分区尾是会悬停的

tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

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

   UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   if (cell == nil) {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }

UITableViewCell的基本属性

// 设置端距,这里表示separator离左边和右边均80像素
tableView.separatorInset = UIEdgeInsetsMake(0,80, 0, 80); 
tableView.separatorStyle =
UITableViewCellSeparatorStyleSingleLine;
tableView.separatorColor = [UIColor blueColor];

image

cell.selectionStyle = UITableViewCellSelectionStyleNone;
/*
我一般会取消选中 也就是使用UITableViewCellSelectionStyleNone
总是感觉有那个选中状态很丑
这里就不一一截图列举选中样式了
UITableViewCellSelectionStyleNone
UITableViewCellSelectionStyleBlue
UITableViewCellSelectionStyleGray
UITableViewCellSelectionStyleDefault
*/


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
/*
UITableViewCellAccessoryNone 没有
UITableViewCellAccessoryDisclosureIndicator 箭头
UITableViewCellAccessoryDetailDisclosureButton 感叹号和箭头
UITableViewCellAccessoryCheckmark 对勾
UITableViewCellAccessoryDetailButton 感叹号
*/

一定要记得删除数据源中的数据,否则指示表面上删除了这一行,下次读取的时候,这行数据又会出现


//左滑之后会出现的字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return @"删除";
    }

//点击了删除 会调用这个方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 从数据源中删除
    [_data removeObjectAtIndex:indexPath.row];
    // 从列表中删除
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];
    }

//退出了编辑模式会走这个方法

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

}

左滑删除

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return 44;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 55;
    }
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 45;
    }
    return CGFLOAT_MIN;
}
// 先设置这三个属性
if (@available(iOS 11.0, *)) { 
   _tableView.estimatedRowHeight = 0;
  _tableView.estimatedSectionHeaderHeight = 0;
   _tableView.estimatedSectionFooterHeight = 0;
}
// 刷新代码放在performWithoutAnimation里
[UIView performWithoutAnimation:^{
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}];

本文没有写到使用 xib 的情况 ,因为个人不太喜欢使用xib。虽然用xib会快一些,但是在后期维护方面并不是十分方便,我也建议大家多使用全代码的形式来写项目。
本文中还有很多没有提及的东西,有一些是忘记了的,如果有疑问直接留言,我会及时添加和修改,谢谢


作者:小_黑_屋
链接:https://www.jianshu.com/p/aa9721e4484d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
上一篇下一篇

猜你喜欢

热点阅读