iOS-OC-设置UITableViewCell之间的间距(推荐第四种)

来源:互联网 发布:js 强行停断点 编辑:程序博客网 时间:2024/04/28 13:32

1:tableView分割线,  不显示

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;



2:iOS-OC-设置UITableViewCell之间的间距(推荐第四种)

原文链接:http://blog.csdn.net/u014220518/article/details/51995989


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


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

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">//协议中的方法,用于返回单元格的大小  
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{  
  3.     return CGSizeMake(ScreenWidth-20,150);  
  4. }  
  5. //协议中的方法,用于返回整个CollectionView上、左、下、右距四边的间距  
  6. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{  
  7.     //上、左、下、右的边距  
  8.     return UIEdgeInsetsMake(10101010);  
  9. }</span></span>  

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

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  2.     return 10;  
  3. }  
  4.   
  5. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  6.     return 1;  
  7. }  
  8.   
  9. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  10.     return 50;  
  11. }  
  12.   
  13. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
  14.     return 10;  
  15. }  
  16.   
  17. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{  
  18.     return 0.00001;  
  19. }  
  20.   
  21. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{  
  22.     UIView *headView = [[UIView alloc]init];  
  23.     headView.backgroundColor = [UIColor redColor];  
  24.     return headView;  
  25. }  
  26.   
  27. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  28.     static NSString *TableSampleIdentifier = @"cellStr";  
  29.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];  
  30.     if (cell == nil) {  
  31.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];  
  32.     }  
  33.     return cell;  
  34. }</span>  



4.重新设置的UITableViewCellframe。

代码如下:

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">#import "MyViewCell.h"  
  2.   
  3. @implementation MyViewCell  
  4.   
  5. - (void)awakeFromNib {  
  6.     [super awakeFromNib];  
  7.     // Initialization code  
  8. }  
  9.   
  10. - (void)setFrame:(CGRect)frame{  
  11.     frame.origin.x += 10;  
  12.     frame.origin.y += 10;  
  13.     frame.size.height -= 10;  
  14.     frame.size.width -= 20;  
  15.     [super setFrame:frame];  
  16. }  
  17.   
  18. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {  
  19.     [super setSelected:selected animated:animated];  
  20.   
  21.     // Configure the view for the selected state  
  22. }  
  23.   
  24. @end</span>  

效果如下:






0 0