还有一个取巧的办法。主要想法是将节号加倍,第一个仅显示headerView,而第二个显示实际单元。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionCount * 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section%2 == 0) {
return 0;
}
return _rowCount;
}
什么需要做的是落实headerInSection代表:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section%2 == 0) {
//return headerview;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section%2 == 0) {
//return headerheight;
}
return 0;
}
这种方法也有你的数据源的影响不大:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int real_section = (int)indexPath.section/2;
//your code
}
与其他方法相比,这种方法是安全的,而不会改变tableview的frame或contentInsets。 希望这可能有所帮助。
是啊...它似乎有点黑客。这看起来像是一个苹果公司的一部分,显然代码是在那里做的,我们只需要能够设置一个标志。 – Tricky 2009-07-02 13:54:26