从单个NSMutableArray向UITableView的2个部分添加数据

3
我想知道如何从单个数据源在tableView中列出不同部分的数据。
我看到的所有示例都有数组数量=节的数量。我想要的是假设我有一个3D nsmutableArray。
If dArray.Id = 1 { //add to first section of UITableView }
else add to Section 2 of UITableView.

可能是可以做到的,但我只需要一个方向。
1个回答

4

是的,这是可以实现的。

您可以使用单个NSMutableArray (resultArray)来存储所有内容。

然后在cellForRowAtIndexPath方法中,您可以这样实现:

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

    if(indexPath.section == 0)
    {
        cell.text=[resultArray objectAtIndex:indexPath.row];
    }
    else if(indexPath.section == 1)
    {
        cell.text=[resultArray objectAtIndex:(indexPath.row+5)];
    }
    else if(indexPath.section == 2)
    {
         cell.text=[resultArray objectAtIndex:(indexPath.row+11)];
    }
}

numberOfRowsInSection方法中

- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section {

    NSInteger count;
    if(section == 0)
    {
        count=6;
    }
    else if(section == 1)
    {
        count=6;
    }
    else if(section == 2)
    {
        count=4;
    }
    return count;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接