I don't get it. The cellForRowAtIndexPath function's indexPath parameter is always 0. I currently have 7 rows in my table. What my table shows is 7x the first table row. What could cause the indexPath to always be zero?
@implementation SitesViewController
@synthesize sites, siteCell;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [sites count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SiteCell"];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"SiteCell" owner:self options:nil];
cell = siteCell;
self.siteCell = nil;
}
Site *site = [sites objectAtIndex:indexPath.section];
UILabel *siteNameLabel;
siteNameLabel = (UILabel *)[cell viewWithTag:1];
siteNameLabel.text = site.siteName;
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSManagedObjectContext *context = [[DigMateAppDelegate sharedAppDelegate] managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForN开发者_如何学运维ame:@"Sites" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
sites = [context executeFetchRequest:request error:&error];
}
Since you have 7 rows and I assume 1 section then you should get record from array based on row index, not section. So the following line in cellForRowAtIndexPath:
method:
Site *site = [sites objectAtIndex:indexPath.section];
should be:
// Use indexPath's row instead of section!
Site *site = [sites objectAtIndex:indexPath.row];
精彩评论