开发者

How to load a tableView with an array of dictionaries?

开发者 https://www.devze.com 2023-03-08 18:15 出处:网络
I want to load a table view with an array which itself has multiple dictionaries. Each of these dictionaries has items which I need to put into a row, one dictionary per row. All of these dictionaries

I want to load a table view with an array which itself has multiple dictionaries. Each of these dictionaries has items which I need to put into a row, one dictionary per row. All of these dictionaries are stored in an array.

How should I perform this task?

Thanks,

See the code below, projects is my array.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   // id <NSFetchedResultsSectionInfo> s开发者_C百科ectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return self.projects.count;
}


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    cell.textLabel.text = [self.projects objectAtIndex:indexPath.row];
}


You can do this with pretty much what you have. You'll just need to change your configureCell:atIndexPath: method to look something like this:

cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:@"some_key"];

Just change some_key to the key in your NSDictionary that you want to display in your UITableViewCell.

You can also change your UITableViewCell style to UITableViewCellStyleSubtitle and then you can add another key from your NSDictionary to cell.detailTextLabel.


In addition to edc1591,

In case if the dictionaries in the array have different keys then

  if([[self.projects objectAtIndex:indexPath.row] count]==1)

   {
     id key= [[[self.projects objectAtIndex:indexPath.row] allKeys] objectAtIndex:0];
     cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:key];
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号