1.設置假的間距,我們在tableviewcell的contentView上添加一個view,比如讓其距離上下左右的距離都是10;這個方法是最容易想到的;
20160722161555120.jpeg
2.用UIContentView來代替tableview狠毯,然后通過下面這個函數(shù)來設置UICollectionViewCell的上下左右的間距;
<span style="font-size:18px;">//協(xié)議中的方法褥芒,用于返回單元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(ScreenWidth-20,150);
}
//協(xié)議中的方法嚼松,用于返回整個CollectionView上、左锰扶、下献酗、右距四邊的間距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//上、左坷牛、下罕偎、右的邊距
return UIEdgeInsetsMake(10, 10, 10, 10);
}</span>
3.用控件tableview,比如有十條數(shù)據(jù)京闰,那就給tableview分十組颜及,每組只放一條數(shù)據(jù),也就是一個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;
}
20160722163217236.png
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];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
20170412165654315.png
感謝:
https://blog.csdn.net/u014220518/article/details/51995989