代码之家  ›  专栏  ›  技术社区  ›  Claudio

iPhone开发-构建手机的问题

  •  0
  • Claudio  · 技术社区  · 16 年前

    我有一个应用程序有很多视图,包括表格视图,但其中一个有一个我不知道如何解决的问题。 当调用单元格的构造函数时:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    参数 cellForRowatindexPath:(nsindexxpath*)索引路径 得到一个随机的奇怪的数字。我用和其他人一样的方式构建这个桌面视图。有人知道发生了什么事吗?

    谢谢, 克劳迪奥

    1 回复  |  直到 16 年前
        1
  •  0
  •   wm_eddie    16 年前

    indexPath的诀窍是使用 UITableView 扩展属性 indexPath.section 和 indexPath.row .

    换句话说,如果您有一个简单的字符串列表:

    self.data = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
    

    然后在 tableView:cellForRowAtIndexPath: 你想做的事:

    cell.textLabel.text = [self.data objectAtIndex:indexPath.row];
    

    如果数组中有数组:

    self.data = [NSArray arrayWithObjects:
                    [NSArray arrayWithObjects:@"A.1", @"A.2", nil],
                    [NSArray arrayWithObjects:@"B.1", @"B.2", nil],
                    nil];
    

    然后你要检查 section 和 row 财产:

    NSArray *strings = [self.data objectAtIndex:indexPath.section];
    cell.textLabel.text = [strings objectAtIndex:indexPath.row];
    

    确保返回正确的数字 numberOfSectionsInTableView: 和 tableView:numberOfRowsInSection: .