组表视图-加载数据

| 寻找有关如何正确加载分组表视图数据的帮助。我正在使用以下代码加载我认为应该为\“ cellForRowAtIndexPath \”准备一个数组“ details \”的数据。但是,当我运行该程序时,所有组的数据都相同,它恰好是数据的最后一行(在名为\“ Users \”的NSArray中)。显然我在做错事,但是不确定...请帮助。
// Customize the number of rows in the table view.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSRange aRange;
aRange.length = 9; aRange.location = 16;
NSString *users = [self.Users objectAtIndex: section];
NSString *subtitle = [NSString stringWithString:[users substringWithRange:aRange]];
details = [[NSArray arrayWithObjects:subtitle, subtitle, subtitle, nil] retain];

return [details count];
} 数据加载例程如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @\"Cell\";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
UIImage *cellImage = [UIImage imageNamed:@\"world.png\"];
cell.imageView.image = cellImage;
cell.textLabel.text = [details objectAtIndex:indexPath.row];
return cell;
}     
已邀请:
根据您的发布,每次表视图向其数据源询问节中的行数时,都会重置“ 3”数组。当最后一次调用
-tableView:numberOfRowsInSection:
时,即最后一节的last3 last被最后设置。设置后,同一数组将为所有部分提供
cell.textLabel.text
的数据。因此,您得到的数据不正确。 计算所有“ 3”个数组并将其存储在另一个数组(例如“ 8”)中是合适的。 然后可以像这样访问数据     
[[detailsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
您的代码的另一个问题是,每次调用
numberOfRowsInSection
时,由于在不释放较早对象的情况下重置
details
时都会泄漏数据。     

要回复问题请先登录注册