viewForHeaderInSection

时间:2015-07-09 07:56:18

标签: ios objective-c uitableview

我有像Instagram这样的UITableView标题自定义我的部分行为标题,在此tableview中我也使用分页来从服务器获取数据。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (currentPage == 0) {
       return 1;

   }

    if (currentPage < totalPages) {
       return myObject.count+1;
    }

    return myObject.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
   return 1;

 }

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section  

 {

    myTimeline = [myObject objectAtIndex:section];
    //the rest of my code to display text in UILabel cell, etc

 }

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

   if (cell.tag == kLoadingCellTag) {
     currentPage++;
    [self getTimeline];
   }    
}

现在问题是,我已经确保我的分页工作正常并且myObject数组计数增加了。 但当我执行滚动时,它会在方法viewForHeaderInSection中崩溃,原因是“索引10超出边界[0 .. 9]”(我在服务器中设置了每页10个项目的分页)

我认为ViewForHeaderInSection中的部分没有使用我已设置的numberOfSectionsInTableView方法进行更新。

那么如何更新ViewForHeaderInSection中的部分?

谢谢..

1 个答案:

答案 0 :(得分:0)

您的numberOfSectionsInTableviewreturn myObject.count+1;,因此表格视图会尽职尽责地询问最后一部分的标题,这超出了myObject数组的范围。您需要移除+1,或者,如果有原因,请在viewForHeaderInSection中包含代码以处理该特殊情况:

if (currentPage > 0 && currentPage < totalPages && section == myObject.count) {
    // create whatever header view is appropriate for the final section
} else {
    myTimeline = ... etc
}