【问题标题】:Index Section in viewForHeaderInSectionviewForHeaderInSection 中的索引部分
【发布时间】:2015-07-09 16:28:56
【问题描述】:

我有 UITableView 标题自定义,如 Instagram,我的部分充当标题,在这个 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 中的部分呢?

谢谢..

【问题讨论】:

  • “我在服务器中设置了每页 10 个项目的分页” - 你的服务器是否总是有 10*k 个条目?

标签: ios objective-c uitableview


【解决方案1】:

您的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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2020-01-24
    • 2016-05-04
    • 2012-06-12
    • 2012-09-06
    相关资源
    最近更新 更多