UITableViewCell使用Autolayout

1.最简单的使用Autolayout的方法:(仅限于列表展示情况,不做任何界面跳转)



(1)这是我cell里面的一些布局大家可以简单尝试一下:

这个 cell 中有 3 个元素,其中 imageView 的 autoLayout 约束为:

imageView 左边离 contentView 左边 0

imageView 上边离 contentView 上边 0

imageView 的 width 和 height 为 80

imageView 下边离 contentView 下边大于等于 0(为了防止内容太少,导致 cell 高度小于图片高度)

Title 的 autoLayout 约束为:

Title 左边离 imageView 右边 8

Title 上边和 imageView 上边在同一只线上

Title 右边离 contentView 右边 0

Title 下边离 description 上边 8

Title 的高度小于等于 22,优先级为 250

Label 的约束为:

Label 左边和 titleLabel 左边在同一直线上

Label 上边里 titleLabel 8

Label 下边里 contentView 下边 0

Label 右边离 contentView 右边 0

(2)创建一个tableView,加入到一个ViewController里面。tableView里面的一些代理自己设置。其中我们常用的一个用于设置cell高度的方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;替换成方法:- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewAutomaticDimension;

}

上面的方法返回一个cell的估计值。或者也可以不写方法直接写句代码self.tableOne.rowHeight = UITableViewAutomaticDimension;

上面的方法可以不用计算cell的高度。大大提高了加载速度;这样说吧比如你有1W个Cell的单元你必须计算1W次的单元格高度的方法。但是使用上面的方法就不用计算了。节省了许多时间;

但是上面的方法在处理界面的跳转的时候(由于系统本身机制的原因)tableView会重新被加载。所以界面返回当前的tableView的时候,table会滑动在最顶端(而不是你点击cell的那个位置)——>所以这个方法只适用于列表展示,而不做其他操作;

2.使用autolayout并且可以做一系列的界面处理(计算高度)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //获取高度
    return [self heightForBasicCellAtIndexPath:indexPath];
}

//获取高度
- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
    //获取一个单例cell,用来计算高度
    static MyCell *sizingCell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self.tableOne registerNib:[UINib nibWithNibName:@"MyCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"mycell"];
        sizingCell=[self.tableOne dequeueReusableCellWithIdentifier:@"mycell"];
    });
    
    //加载单例cell的数据(self.items是文字的数组)
    sizingCell.contentLabel.text = [self.items objectAtIndex:indexPath.row];
    
    //设置cell中的label的约束的最大值, 如果没设置, 在[sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]这个得到的高度不够准确, 所以需要具体设置的宽度,以便能够获取准确地值
    sizingCell.contentLabel.preferredMaxLayoutWidth = ScreenWidth-106;
    
    //可以直接在这里计算 但是我不建议, 因为这样子也有可能造成得到的高度不准确, 建议用下面注释掉的return  但也要看具体情况了, 说不定通过下面那个也会不准确
    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
   
    return size.height + 1.0f;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS-RATreeView是一个开源的第三方库,提供了多层级的UITableView展示功能。使用该库可以轻松实现多级列表的展开与收起。 首先,在项目中引入iOS-RATreeView库。可以使用CocoaPods引入,也可以手动下载并导入。 接下来,在需要使用多级列表的UIViewController中,创建一个RADataObject类型的数组,用来存储数据。RADataObject是iOS-RATreeView中的一个数据模型,用来表示一条记录。每个RADataObject可以包含多个子RADataObject,从而形成多级列表。 ``` // 创建RADataObject数组 NSMutableArray *data = [NSMutableArray array]; // 创建一级列表 RADataObject *level1_1 = [RADataObject dataObjectWithName:@"Level 1-1" children:nil]; RADataObject *level1_2 = [RADataObject dataObjectWithName:@"Level 1-2" children:nil]; RADataObject *level1_3 = [RADataObject dataObjectWithName:@"Level 1-3" children:nil]; // 创建二级列表 RADataObject *level2_1 = [RADataObject dataObjectWithName:@"Level 2-1" children:nil]; RADataObject *level2_2 = [RADataObject dataObjectWithName:@"Level 2-2" children:nil]; RADataObject *level2_3 = [RADataObject dataObjectWithName:@"Level 2-3" children:nil]; // 将二级列表添加到一级列表中 level1_1.children = @[level2_1, level2_2]; level1_2.children = @[level2_3]; // 将一级列表添加到RADataObject数组中 [data addObject:level1_1]; [data addObject:level1_2]; [data addObject:level1_3]; ``` 创建完数据源后,需要创建RATreeView对象,并设置代理和数据源。 ``` // 创建RATreeView对象 self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; // 设置代理和数据源 self.treeView.delegate = self; self.treeView.dataSource = self; ``` 接下来实现RATreeViewDataSource协议中的方法,用来返回列表的数据。具体实现可以参考下面的代码。 ``` - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { static NSString *identifier = @"Cell"; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 获取RADataObject对象 RADataObject *dataObject = item; // 设置cell的文本 cell.textLabel.text = dataObject.name; return cell; } - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点数量 return self.data.count; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点数量 return dataObject.children.count; } } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点 return self.data[index]; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点 return dataObject.children[index]; } } - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { // 返回是否可以编辑 return YES; } - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除节点 RADataObject *parentObject = [treeView parentForItem:item]; if (parentObject) { NSMutableArray *children = [NSMutableArray arrayWithArray:parentObject.children]; [children removeObject:item]; parentObject.children = children; } else { NSMutableArray *data = [NSMutableArray arrayWithArray:self.data]; [data removeObject:item]; self.data = data; } // 刷新列表 [treeView reloadData]; } } ``` 最后,在RATreeViewDelegate协议中实现展开与收起节点的方法。 ``` - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = YES; } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = NO; } ``` 至此,多级列表展开与收起的功能就实现了。在需要展示多级列表的地方,只需要将创建的RATreeView添加到视图中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值