从单个nsmutablearray中向UITableView的两个部分添加数据

时间:2021-10-05 15:44:56

I wanted to know how to list data in tableView in different sections BUT from a single datasource.

我想知道如何在tableView中列出不同部分的数据,但是只列出一个数据源。

All examples i saw had number of arrays = number of sections. What i want is suppose I have a 3d nsmutableArray.

我看到的所有示例都有数组数=分段数。我想要的是一个3d的nsmutableArray。

If dArray.Id = 1 { //add to first section of UITableView }
else add to Section 2 of UITableView.

Its probably dooable, but i jus need a direction.

这可能可行,但我需要一个方向。

1 个解决方案

#1


4  

Yeah it is possible to do it.

是的,这是可能的。

You can have a single NSMutableArray (resultArray) with the entire content in it.

您可以使用一个包含所有内容的NSMutableArray (resultArray)。

Then in the cellForRowAtIndexPath method you can do it this way

然后在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)];
    }
}

and in numberOfRowsInSection

而在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;
}

#1


4  

Yeah it is possible to do it.

是的,这是可能的。

You can have a single NSMutableArray (resultArray) with the entire content in it.

您可以使用一个包含所有内容的NSMutableArray (resultArray)。

Then in the cellForRowAtIndexPath method you can do it this way

然后在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)];
    }
}

and in numberOfRowsInSection

而在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;
}