gpt4 book ai didi

ios - cellForRowAtIndexPath:indexPath始终等于0

转载 作者:行者123 更新时间:2023-12-01 17:27:56 25 4
gpt4 key购买 nike

我不明白cellForRowAtIndexPath函数的indexPath参数始终为0。我的表当前有7行。我的表格显示的是表格第一行的7倍。是什么导致indexPath始终为零?

@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];
}

最佳答案

由于您有7行并且我假设有1个部分,因此您应该基于行索引而不是部分从数组中获取记录。因此,cellForRowAtIndexPath:方法中的以下行:

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

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

关于ios - cellForRowAtIndexPath:indexPath始终等于0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com