一、设置假间距
在tableViewCell的contentView 上添加一个view,通过这个view设置间距,所有内容都放到这个view 上

缺点:
如果存在左滑删除,左滑删除按钮的高度是整个cell的高度,看起来和cell内容高度不匹配

二、通过设置tableview section间距,来实现cell间距
比如tableview有10条数据,就设置为10个section,设置section的headerview footerview的高度来实现cell间距,但这种方式只能设置上下间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
缺点:
1.只能设置上下间距
2.如果section headerView或footerView有内容时,此时就无法实现cell间距
三、使用UICollectionView代替UITableView,通过设置UICollectionViewCell的上下左右的间距,来实现Cell间距
//协议中的方法,用于返回单元格的大小
- (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);
}
缺点:
对于UICollectionView,系统没有相关提供方法来实现左滑删除,需要自定义。较为复杂
四、重写cell的setFrame方法,实现cell间距
通过调整cell的frame,来实现cell的间距
- (void)setFrame:(CGRect)frame {
frame.origin.x += 16;
frame.size.width -= 32;
frame.size.height -= 8;
[super setFrame:frame];
}
缺点:
左滑删除时,cell滑动到最右端松手,会有一点卡顿,再回弹到正确的位置
网友评论