【发布时间】:2012-04-20 01:46:05
【问题描述】:
我在 Interface Builder 中创建了一个静态表,其中包含 6 个部分,所有部分的行数都不同。 我现在想添加具有不同行数的第 7 部分。
首先,一旦我取消注释 Xcode 插入的标准表委托方法,我就会在 self.tableView.tableHeaderView = containerView;我在表格中添加了标题。
更重要的是,下面的代码让我崩溃了
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==6) {
return 4;
} else {
return [super tableView:tableView numberOfRowsInSection:section];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{/*
if (indexPath.section == 6) {
static NSString *CellIdentifier = @"cellWireless";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}*/
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
我如何正确地保留现有部分,但添加一个带有几个单元格的额外部分?
【问题讨论】:
-
您必须检查每个部分的行数。否则您应该将更新的数据模型发送到您的 tableview 以使用更新的数据重新加载数据。
-
确实返回 [super tableView:tableView numberOfRowsInSection:section];不返回每个部分的行数?
-
上述方法是当您使用原型单元格时(dequeueReusableCellWithIdentifier)。您正在使用静态单元格。
-
"...根据单元格是用于静态行内容还是动态行内容,采用两种方法之一。对于动态内容,表格视图是一个列表,其中包含大量(并且可能是无限的)行。对于静态内容,行数是有限的、已知的数量..." 来源:developer.apple.com/library/ios/#documentation/UserExperience/…
-
我在上一篇文章中的devforums.apple.com/message/502990#502990 找到了解决方案。您必须覆盖每个 tableview 委托方法。
标签: objective-c xcode cocoa-touch uitableview