chr*_*o16 24 iphone objective-c uitableview
我有一个分组的UITableView,其中并非所有部分都可以一次显示,该表由一些数据驱动,而不是每个记录都有.我的麻烦是,没有某些部分的记录在表格中显示为空白(见图)
替代文字http://img220.imageshack.us/img220/5633/screenshot20100322at228.png
没有页脚/标题.我错过了什么?
- (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;
}
Run Code Online (Sandbox Code Playgroud)
use*_*178 41
我有类似的情况,如果我可以让它有空的部分我的数据模型真的更好.
如果在tableview控制器中进行设置self.tableView.sectionHeaderHeight = 0;,则可以解决问题self.tableView.sectionFooterHeight = 0;.这些必须为0,因为委托方法以某种方式忽略返回值0.然后您必须覆盖一些委托方法:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section==0)
return sectionGapHeight;
if ([self tableView:tableView numberOfRowsInSection:section]==0) {
return 0;
}
return sectionGapHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section==[self numberOfSectionsInTableView:tableView]-1) {
return sectionGapHeight;
}
return 0;
}
- (UIView *)sectionFiller {
static UILabel *emptyLabel = nil;
if (!emptyLabel) {
emptyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
emptyLabel.backgroundColor = [UIColor clearColor];
}
return emptyLabel;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [self sectionFiller];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [self sectionFiller];
}
Run Code Online (Sandbox Code Playgroud)
此代码还将使节之间的间隙与第一节之前的间隙和最后一节之下的间隙的高度相同.在我看来,这比默认情况看起来更好,其中间隙是顶部和底部之间的差距的两倍.
Ram*_*esh 17
这是适合我的解决方案
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
switch (section) {
case 1: case 3: case 5: return 20.0f; break;
default: return 0.01f; // for some reason 0 is not accepted - give something :)
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
switch (section) {
case 1: case 3: case 5: return 20.0f; break;
default: return 0.01f; // for some reason 0 is not accepted - give something :)
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的屏幕截图中,您的numberOfSectionsInTableView:方法返回的值是5吗?然后你的getRowCount:方法(调用from numberOfRowsInSection:)为那些"缺失"的部分(例如1,3和4)返回值0?
如果您不希望介于两者之间,可能的解决方案是仅声明您希望显示的部分数.在上面的示例中,您只想从中返回值3 numberOfSectionsInTableView:.
此解决方案基于 user362178 解决方案。但保持部分的标签完好无损:
在代码顶部的某处定义:
#define HEIGHT_FOR_HEADER_AND_FOOTER 50
Run Code Online (Sandbox Code Playgroud)
添加到视图确实加载:
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;
Run Code Online (Sandbox Code Playgroud)
添加这些方法:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// Needed because if this does not return a value, the header label of the section will
// be at the top of the screen.
if (section == 0)
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
if ([self tableView:tableView numberOfRowsInSection:section] == 0)
{
return 0;
}
else
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// if last section
if (section == [self numberOfSectionsInTableView:tableView] - 1)
{
return HEIGHT_FOR_HEADER_AND_FOOTER;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14009 次 |
| 最近记录: |