2011-04-17 86 views
0

寻找有关如何正确加载分组表格视图的帮助。我正在使用以下代码加载我认为应为“cellForRowAtIndexPath”准备数组“细节”的数据。然而,当我运行程序时,我得到了所有组的相同数据,它恰好是我数据中的最后一行(在NSArray中称为“用户”)。显然我做错了什么,但不知道什么...请帮助。组表查看 - 加载数据

// Customize the number of rows in the table view. 

- (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{

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; 

}

回答

0

从您发布的内容中可以看出,details数组在每次表视图询问其数据源中的某一节中的行数时正在被重置。 details最后设置为-tableView:numberOfRowsInSection:最后一次被调用时,即最后一段。一旦设置,相同的阵列为所有部分提供cell.textLabel.text的数据。所以你得到不正确的数据。

将所有details阵列计算并存储在不同的阵列中,例如detailsArray将是适当的。然后
的数据可以访问他们像
[[detailsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]

与您的代码的另一个问题是,每次numberOfRowsInSection被调用时,数据被泄露作为details正在复位而不释放较早的对象。