OC 如何设置UITableViewCell之间的间距

本文介绍四种UITableViewCell间距调整的方法:直接修改UITableViewCellFrame实现间距调整、利用UITableView分组特性及头尾视图设置间距、使用UICollectionView布局调整间距以及在UITableViewCell contentView中添加自定义视图来模拟间距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.重新设置的UITableViewCellframe

<span style="font-size:18px;">#import "MyViewCell.h"  
  
@implementation MyViewCell  
  
- (void)awakeFromNib {  
    [super awakeFromNib];  
    // Initialization code  
}  
  
- (void)setFrame:(CGRect)frame{  
    frame.origin.x += 10;  
    frame.origin.y += 10;  
    frame.size.height -= 10;  
    frame.size.width -= 20;  
    [super setFrame:frame];  
}  
  
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {  
    [super setSelected:selected animated:animated];  
  
    // Configure the view for the selected state  
}  
  
@end</span> 

2.用控件tableview,比如有十条数据,那就给tableview分十组,每组只放一条数据,也就是一个cell,然后设置UICollectionViewCell的head view和foot view来设置cell的间距,但是这个方法只能设置上下间距,如果想设置距离屏幕左右的距离,可以设置uitableview距离左右的距离;uitableview的style为UITableViewStyleGrouped;不然headview会浮动;

<span style="font-size:18px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
    return 10;  
}  
  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
    return 1;  
}  
  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
    return 50;  
}  
  
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
    return 10;  
}  
  
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{  
    return 0.00001;  
}  
  
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{  
    UIView *headView = [[UIView alloc]init];  
    headView.backgroundColor = [UIColor redColor];  
    return headView;  
}  
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
    static NSString *TableSampleIdentifier = @"cellStr";  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];  
    if (cell == nil) {  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];  
    }  
    return cell;  
}</span>

3.用UIContentView来代替tableview,然后通过下面这个函数来设置UICollectionViewCell的上下左右的间距

 //协议中的方法,用于返回单元格的大小  
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{  
        return CGSizeMake(ScreenWidth-20,150);  
    }  
    //协议中的方法,用于返回整个CollectionView上、左、下、右距四边的间距  
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{  
        //上、左、下、右的边距  
        return UIEdgeInsetsMake(10, 10, 10, 10);  
    }

4.设置假的间距,我们在tableviewcell的contentView上添加一个view,比如让其距离上下左右的距离都是10;这个方法是最容易想到的;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值