Reputation: 177
What i am trying to do is parse an XML and have the elements populate the table view. The XML parses fine and prints in console, but is unable to display in the table view. Here is my code for populating a cell:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:@"title"]];
[cell.detailTextLabel setText:[news objectForKey:@"link"]];
[cell.detailTextLabel setNumberOfLines:2];
}
return cell;
}
Upvotes: 3
Views: 1138
Reputation: 857
Fill the data After creating creating or reusing your cell . It will work fine now ,Use below code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
[cell.textLabel setText:[news objectForKey:@"title"]];
[cell.detailTextLabel setText:[news objectForKey:@"link"]];
[cell.detailTextLabel setNumberOfLines:2];
return cell;
}
Upvotes: 1
Reputation: 177
I got it to display. Seems I forgot to set the data source in the viewDidLoad method... thank you everyone for all the help.
Upvotes: 3
Reputation: 25946
You are only adding data to the text labels when you create a new one. Add the data to the view after the if statement.
UITableView will reuse cells when possible. When it does you will get an existing cell from dequeueReusableCellWithIdentifier:
.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
NSMutableDictionary *news = (NSMutableDictionary*) [feeds objectAtIndex:indexPath.row];
cell.textLabel.text = [news objectForKey:@"title"];
cell.detailTextLabel.text = [news objectForKey:@"link"];
cell.detailTextLabel.numberOfLines = 2;
return cell;
}
Upvotes: 1