1
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编程的初学者

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

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

2 回答 2

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;
}
于 2013-04-19T12:53:44.153 回答
1

把它放在 cellForRowAtIndexPath 方法中。

cell.textLabel.text = [[objects objectAtIndex:indexPath.row] valueForKey:@"title"];
于 2013-04-19T12:54:58.167 回答