2011-08-02 9 views
1

나는 이해하지 못합니다. cellForRowAtIndexPath 함수의 indexPath 매개 변수는 항상 0입니다. 현재 테이블에 7 개의 행이 있습니다. 내 테이블에 표시된 것은 첫 번째 테이블 행의 7 배입니다. 무엇이 indexPath를 항상 0으로 만들 수 있습니까?cellForRowAtIndexPath : indexPath는 항상 0입니다.

@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 entityForName:@"Sites" inManagedObjectContext:context]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDesc]; 
    NSError *error; 
    sites = [context executeFetchRequest:request error:&error]; 
} 

답변

8

너는 7 개의 행을 가지고 있으므로 1 섹션으로 가정하므로 섹션이 아닌 행 인덱스를 기반으로 배열에서 레코드를 가져와야합니다. 그래서 cellForRowAtIndexPath: 방법에 다음 줄은 :

Site *site = [sites objectAtIndex:indexPath.section]; 

은 다음과 같아야합니다

// Use indexPath's row instead of section! 
Site *site = [sites objectAtIndex:indexPath.row]; 
+0

트릭을했다가, 감사합니다! –

관련 문제