怎么设置tableviewcell的间距之间的间距

怎么设置tableViewCell之间的间距_百度知道2620人阅读
iOS(384)
两种方式,看你的tableView是哪种样式。
如果是UITableViewStylePlain,你可以将UITableViewCell的separator设置为UITableViewCellSeparatorStyleNone。将tableViewCell的背景色,contentView的背景色设置为透明,在contentView中添加一个UIImageView做为背景,使UIImageView的高度小于cell的高度,这样创建出的tableView就可以使每个cell之间看起来有一定间隔。这个间隔就是imageView的高度与cell的高度之间的差值。
如果你的tableView样式是UITableViewStyleGrouped,那就更好办了,因为group的tableView每个section都是隔开的,你只需要给每个section添加一行cell就行了。如果需要调整section之间的距离,你可以用UITableView的属性sectionHeaderHeight或sectionFooterHeight来设置。也可以用delegate方法来设置高度,heightForHeaderInSection或heightForFooterInSection
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:778480次
积分:9046
积分:9046
排名:第1419名
原创:19篇
转载:1202篇
评论:44条
(35)(159)(31)(55)(22)(18)(26)(25)(4)(11)(17)(9)(25)(13)(34)(9)(2)(6)(12)(4)(20)(25)(36)(24)(18)(9)(6)(13)(3)(12)(4)(11)(3)(1)(4)(3)(4)(9)(5)(5)(20)(5)(5)(14)(10)(45)(81)(117)(7)(3)(23)(20)(105)(45)iOS_Objective-C(23)
系统的TableviewCell之间是没有间距的,我们没法改变,那应该怎么来实现呢?
通过设置cell的contentView来实现间接,在cell的contentView的顶部或者底部留下一定的间距,这样就会有cell间就有间距的效果。但是这种方式在cell有点击效果的时候,会很明显的看出有分层,因为这时候cell是被点击的,contentView都会有系统点击的阴影效果。这种方式在cell左滑删除,置顶等操作的时候,左滑出的视图会高出一部分(左滑显示出的高度=(cell的高度-留下的间距高度)+ 留下的间距高度[我们不需要的])
通过分组的方式间接的实现,每组的Header可以当做是cell之间的间距,每组中只有一个cell(数据显示也会比较简单的)。废话不多说上代码!
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
但是呢,这还是会出现一个问题,因为系统默认分组的时候每组的Header会停留在tableview的顶部,这要怎么处理呢?
//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView)
CGFloat sectionHeaderHeight = 10
if (scrollView.contentOffset.y &= sectionHeaderHeight && scrollView.contentOffset.y &= 0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0)
} else if (scrollView.contentOffset.y &= sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0)
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9099次
排名:千里之外
原创:36篇
(1)(1)(1)(18)(3)(3)(12)UITableview的一个section下的各行Row之间可以设置间隔一段距离吗?_问答_ThinkSAAS
UITableview的一个section下的各行Row之间可以设置间隔一段距离吗?
UITableview的一个section下的各行Row之间可以设置间隔一段距离吗?
RT,UITableview delegate中貌似只可以设置row的高度,有没有方法让我设置ow与row之间的距离?
你给每个Cell留空一段不就行了?
要不就多设置一倍的number of rows,然后隔行插入一个空白cell
你不能改变cell得高度 只能再插入一个空白cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CELL_ID2 = @"SOME_STUPID_ID2";
// even rows will be invisible
if (indexPath.row % 2 == 1)
UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];
if (cell2 == nil)
cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CELL_ID2];
[cell2.contentView setAlpha:0];
[cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
return cell2;
[ccTableView setBackgroundColor:[UIColor clearColor]];
cardsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cardsCell"];
if(cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cardsCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
// Use indexPath.row/2 instead of indexPath.row for the visible section to get the correct datasource index (number of rows is increased to add the invisible rows)
NSString *nmCard = [[self.cards valueForKeyPath:@"cards.name"] objectAtIndex:(indexPath.row/2)];
cell.descCardLabel.text = nmC
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// two times minus one (invisible at even rows =& visibleCount == invisibleCount+1)
return [[self.cards valueForKeyPath:@"cards"] count] * 2 - 1;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
if (indexPath.row % 2 == 1)
return 40;
return 162;
你可以自定义一个View,将这个View的高度设的高一点,然后把这个View作为cell的backgroundView。这样就能实现你说的那个效果了。
添加你想要问的问题
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信IOS 各位大神帮帮忙! UITableView 中单元格如何设置间距 左右间距 、上下间距。_百度知道

我要回帖

更多关于 table cell 间距 的文章

 

随机推荐