简体   繁体   中英

Need Help Setting Cell's TextLabel From NSDictionary

So I have an NSDictionary with 4 objects in it. Each of these objects then has 2 properties, name and date

I can list out all the data by logging it.

I am assuming your sample code is inside of this method:

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

If not, that is your first problem.

Right now, you are probably sequentially setting each cell to have every title one by one, until it gets to the end of the list. All of the cells have the same title because they all have the last title in the list.

You should drop the for-loop and use this instead:

NSDictionary *object = [objects objectAtIndex:[indexPath row]];
NSString *title = [object objectForKey:@"title"];
NSString *date = [object objectForKey:@"publishedDate"];
cell.textLabel.text  = title;
cell.detailTextLabel.text  = date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM