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

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

<span style="font-size:18px;">//协议中的方法,用于返回单元格的大小
- (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);
}</span>

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

- (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;
}

4.重新设置的UITableViewCellframe。

代码如下:

#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];
}

@end

最新文章

  1. 基于Netty打造RPC服务器设计经验谈
  2. linux shell取文本最后一行
  3. OutputCache属性详解(一)一Duration、VaryByParam
  4. 使用Jaxb2进行xml与bean的转义时Date的format设置
  5. Linux Ubuntu搭建git服务器
  6. PHP 设计模式之观察者模式
  7. linux lamp服务器安装配置
  8. hdu 4101
  9. HTML中属性ID和属性NAME的区别(转)
  10. bkViewer V4.9k(数码照片浏览工具)中文免费版
  11. sql server获取当前年月日 时分秒
  12. SpringMVC源码情操陶冶-FreeMarker之web配置
  13. 常用数学符号的 LaTeX 表示方法
  14. error:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
  15. 使用IDEA配置Maven + SpringMVC + Mybatis 【一步一步踩坑详细配置完成】
  16. NodeJs之EXCEL文件导入导出MongoDB数据库数据
  17. 3.2station
  18. C#求一组数的众数
  19. Security.ssl-pinning
  20. jupyter notebook不能选择虚拟环境的解决方法

热门文章

  1. Angular 2 ViewChild &amp; ViewChildren
  2. android TextView selector点击样式改变
  3. Elasticsearch集成ik分词器
  4. 希尔排序(java)
  5. Maven查找添加方式
  6. 《SpringBoot揭秘 快速构建微服务体系》读后感(一)
  7. JAVA正则表达式之 Pattern介绍
  8. VMWare虚拟机Bridged类型网卡ping不通的原因和解决办法
  9. HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)
  10. Xilinx SDSoc 加载opencv库