简体   繁体   中英

Adding data to 2 Sections of UITableView from SINGLE nsmutablearray

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

All examples i saw had number of arrays = number of sections. What i want is suppose I have a 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.

Yeah it is possible to do it.

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

Then in the cellForRowAtIndexPath method you 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM