【问题标题】:iPhone: UITableView sectionsiPhone:UITableView 部分
【发布时间】:2012-02-29 12:39:40
【问题描述】:

我有一个带有部分的表格视图。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return numberHigherThen0;
}

在 cellForRowAtIndexPath 方法中,我根据部分设置行

- (UITableViewCell *)tableView:(UITableView *)aTableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   if (0 == indexPath.section) {
       //setup rows
   } esle {
     //setup rows
   }
}

节和节中的行数动态变化。但是部分的数量总是 > 0。所以在 cellForRowAtIndexPath indexPath.section == 0 中总会有这种情况,对吧?但有时 cellForRowAtIndexPath 没有通过 case indexPath.section == 0 !!!为什么 ?什么时候能成真? cellForRowAtIndexPath 什么时候可以从 1 开始计数?

更新: 第 2 节没有行

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    numberOfSectionsInTableView: 中,您可以指定节的数量 - 但是,节从 0 开始编号。

    因此,如果您在 numberOfSectionsInTableView 中返回 3,您将拥有三个部分 - 第 0 部分、第 1 部分和第 2 部分。

    【讨论】:

    • 那么为什么 indexPath.section == 0 有时永远不会正确?
    • 该部分可能不可见,或者该部分可能根本没有行?
    • 该部分可见并且有 1 行
    【解决方案2】:

    tableView:cellForRowAtIndexPath: 仅在表格视图需要刷新给定索引路径的单元格时调用。

    因此,如果在您的应用启动时第 0 部分可见,是的,您应该至少收到一次带有 section == 0 的调用。在后续的刷新中,如果 UITableView 认为不需要刷新,可能会跳过第 0 节。这可能就是您所看到的。

    使用reloadRowsAtIndexPaths:withRowAnimation:reloadSections:withRowAnimation:等告诉UITableView某行发生了变化,需要刷新。

    【讨论】:

    • "因此,如果在您的应用程序启动时第 0 部分可见,是的,您应该至少收到一次使用 section == 0 的调用。" 但我没有,这就是问题
    • 您是否为此部分向tableView:numberOfRowsInSection: 返回>0 号码?如果该部分没有行,则 UITableView 不会尝试为其获取行...
    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多