如何将数据从NSDictionary对象加载到表格单元格

时间:2013-04-19 12:35:31

标签: uitableview core-data ios6

I Want to load the Table data from a NSDictionary object. This is how i did below function to fetch numberofRows. how to set the value for the cell from NSDictionary object ?

请给我解决方案,我是Objective C编程的初学者

以下是我的NSDictionary对象“obj”,我从coredata获取。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSEntityDescription *entity = [NSEntityDescription  entityForName:@"Pictures" inManagedObjectContext:managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    [request setResultType:NSDictionaryResultType];
    [request setReturnsDistinctResults:YES];
    [request setPropertiesToFetch:@[@"title"]];

    // Execute the fetch.
    NSError *error;

  objects = [managedObjectContext executeFetchRequest:request error:&error];
    if (objects == nil) {
        // Handle the error.
    }
    for( obj in objects ) {
        NSLog(@"Title: %@", [obj objectForKey:@"title"]);
    }

    return [objects count];
}

在单元格中,我想添加NSdictionary对象中的标题。

- (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];
    }     

    return cell;
}

2 个答案:

答案 0 :(得分:2)

你可以这样做,可能是它的工作是我的朋友: -

- (void)viewWillAppear:(BOOL)animated
{
[self getdata];
[yourtable reloadData];

 [super viewWillAppear:animated];

}

-(void)getdata
{

 NSEntityDescription *entity = [NSEntityDescription  entityForName:@"Pictures" inManagedObjectContext:managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    [request setResultType:NSDictionaryResultType];
    [request setReturnsDistinctResults:YES];
    [request setPropertiesToFetch:@[@"title"]];

    // Execute the fetch.
    NSError *error;

  objects = [managedObjectContext executeFetchRequest:request error:&error];
    if (objects == nil) {
        // Handle the error.
    }
    for( obj in objects ) {
        NSLog(@"Title: %@", [obj objectForKey:@"title"]);
    }


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [objects 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];

      cell.textlabel.text = [[objects objectatindex:indexpath.row] valueforkey@"title"];
    }     

    return cell;
}

答案 1 :(得分:1)

将它放在cellForRowAtIndexPath方法中。

cell.textLabel.text = [[objects objectAtIndex:indexPath.row] valueForKey:@"title"];