Adding data to 2 sections of UITableView from SINGLE nsmutablearray

I wanted to know how to list data in a View table in different sections, but from the same data source.

All the examples I saw had the number of arrays = the number of sections. I want my 3D nsmutableArray.

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

This is probably possible, but I need a direction.

+3
source share
1 answer

Yes it is possible.

You can have one NSMutableArray( resultArray) with all the contents in it.

Then in the method cellForRowAtIndexPathyou can do it this way

- (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

- (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;
}
+4
source

Source: https://habr.com/ru/post/1718700/


All Articles