Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Covington
15,290 PointsObjective-C(onfusion): different simulators show different # of rows/section in table view
I'm working with the Master-Detail template in the blog reader project. I've got several sections in my master table view, each with 1 - 4 rows. Everything shows up as expected in the 4" 64bit iPhone simulator, but when I switch to the 3.5" or 4" simulators, only the first row per section is displayed. Any thoughts as to what might be happening would be appreciated!
EDIT: I was able to get things to work properly for all simulators by changing two lines in this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *sectionYear = [self.years objectAtIndex:section];
int journalCountForYear = 0;
for (NSDictionary *dict in self.journalArticles) {
if ([dict valueForKey:@"year"] == sectionYear) {
journalCountForYear++;
}
}
return journalCountForYear;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *sectionYear = [self.years objectAtIndex:indexPath.section];
NSMutableArray *journalArticlesInSection = [NSMutableArray array];
for (NSDictionary *dict in self.journalArticles) {
if ([dict valueForKey:@"year"] == sectionYear) {
[journalArticlesInSection addObject:dict];
}
}
NSString *title = [journalArticlesInSection[indexPath.row] valueForKey:@"title"];
NSString *authors = [journalArticlesInSection[indexPath.row] valueForKey:@"authors"];
cell.textLabel.text = title;
cell.detailTextLabel.text = authors;
return cell;
}
I changed both occurrences of this:
[dict valueForKey:@"year"] == sectionYear
to this:
[dict[@"year"] integerValue] == [sectionYear integerValue]
Does anyone know why this would cause problems for the 32bit sims, but not the 64bit one?
1 Answer
Darryl Johnson
818 PointsWhen comparing strings DO NOT use == in Objective-C. The == is comparing pointer addresses not the actual value of the strings. Instead use isEqualToString:(NSString).
NSString *sectionYear = [self.years objectAtIndex:section];
if ([sectionYear isEqualToString:[dict valueForKey:@"year"]]) {
journalCountForYear++;
}