简体   繁体   中英

Filling UITableview with NSMutableArray

Trying to fill my UItableview with my Array. I would like to match a object in my array to the correct cell.

My array is as follows.

(
        (
                {
            cost = 160;
            height = 1;
            room_number = 1;
            square_size = 1;
            title = "TIMBER BLINDS";
            width = 1;
        }
    ),
        (
                {
            cost = 170;
            height = 1;
            room_number = 2;
            square_size = 1;
            title = "ROMAN BLINDS";
            width = 1;
        }
    )

My question is how do i match the correct title to the cell based on the room_number in my array ?

Thanks

It seems like you are having array of array which contains dictionary so firstly fetch the object from an array by indexPath.row. The fetched object is also an array. Fetch dictionary object from it by objectAtIndex. Now that you have accessed dictionary so you can access your key value.

NSMutableArray *fetchedArray= [yourArray objectAtIndex:indexPath.row];
NSDictionary *fetchedDictionary = [fetchedArray objectAtIndex:0];

NSNumber *roomNumber= [fetchedDictionary valueForKey:@"room_number"];
if([roomNumber intValue] == indexPath.row) {
    [cell setTextLabel:[fetchedDictionary valueForKey:@"title"]];
}

Assuming your array is self.tableContent

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *object = self.tableContent[index.section][index.row];

     ..... some cell code
     cell.title = object[@"title"];
}

It is dictionary of array of array

you can get value of "room_number" by

NSString *room_number = [[[myarray1 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"room_number"]

() -- this represents array {} -- this represents dictionary

So you can see you have an array of arrays and each array contains a single object and ie a dictionary.

In terms of Objective C, it is like this:

NSDictionary *dict1 = //first dictionary
NSDictionary *dict2 = //second dictionary

NSArray *mainArray = [NSArray arrayWithObjects: [NSArray arrayWithObject:dict1],[NSArray arrayWithObject:dict2], nil];

To retrieve value inside key title from dictionary and display that in TableView:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// other code to set up cell
NSDictionary *array =[[mainArray objectAtIndex:indexPath.row] objectAtIndex:0];
cell.textLabel.text = [array objectForKey:@"title"];
return cell;
}

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