還有一個取巧的辦法。主要想法是將節號加倍,第一個僅顯示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