krish
krish

Reputation: 641

How to load Data from NSDictionary object to Table Cell

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 ?

Please provide me solution , I am begineer to Objective C programming

Following is my NSDictionary object "obj" which i am fetching from 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];
}

Here in the cell i want to add the title which are there in NSdictionary object .

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

Upvotes: 1

Views: 716

Answers (2)

Put this in cellForRowAtIndexPath Method.

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

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49730

you can do like this way may be its work's my Friend:-

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

Upvotes: 2

Related Questions