当节为空时,分组的UITableView显示空白区域 [英] Grouped UITableView shows blank space when section is empty

查看:122
本文介绍了当节为空时,分组的UITableView显示空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分组的UITableView,其中并非所有部分都可以同时显示,该表由一些并非每条记录都有的数据驱动。我的麻烦是,没有某些部分的记录在表格中显示为空格(见图片)

I have a grouped UITableView where not all sections may be displayed at once, the table is driven by some data that not every record may have. My trouble is that the records that don't have certain sections show up as blank spaces in the table (see picture)

替代文字http://img220.imageshack.us/img220/5633/screenshot20100322at228.png

没有页脚/标题。我错过了什么?

There are no footers/headers. Anything I've missed?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self getRowCount:section];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [NSString stringWithFormat:@"section: %d row: %d",indexPath.section,indexPath.row];
// Configure the cell...

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    float height = 0.0;

    if([self getRowCount:indexPath.section] != 0){
        height = kDefaultRowHeight;
    }

    return height;

}


推荐答案

在上面的截图,是你的 numberOfSectionsInTableView:方法返回值为5?然后是你的 getRowCount:方法(从 numberOfRowsInSection:调用)为那些缺失部分返回值0 (例如1,3和4)?

In the above screenshot, is your numberOfSectionsInTableView: method returning a value of 5? And then is your getRowCount: method (called from numberOfRowsInSection:) returning a value of 0 for those "missing" sections (e.g. 1, 3 and 4)?

如果您不希望介于两者之间的间隙,可能的解决方案是仅声明您想要显示的部分数量。在上面的示例中,您只想从 numberOfSectionsInTableView:返回值3.

If you don't want gaps in between, the likely solution is to only declare the number of sections you want to be visible. In your example above, you'd only want to return value of 3 from numberOfSectionsInTableView:.

这篇关于当节为空时,分组的UITableView显示空白区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆