Uitableview无法保存数据
问题描述:
嗨,我有一个uitableview,我显示了一些约200行的数据。数据将以几个部分显示,这些部分将在运行时确定。我能够得到部分,但我无法以正确的顺序显示特定部分的数据。我有一个数组字典形式的数据。Uitableview无法保存数据
我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TeraGoAppDelegate *appDel = (TeraGoAppDelegate *)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
cell.textLabel.text = [(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:countEqpIndex] objectForKey:@"EQP_NAME"];
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
if(![[(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:indexPath.row] objectForKey:@"select"] isEqualToString:@"0"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Set up the cell...
return cell;
}
我想在每一个部分使用indexPath.row但其值初始化为0我无法从阵列中的数据在这种情况下我没有获取数据的数组索引。我将如何获得我需要显示的值的数组的索引?
更新:
[dictComp setObject:arrEqps forKey:CompName];
[arrCompEqp addObject:dictComp];
[arrEqps removeAllObjects];
我使用上面的代码在表视图阵列添加数据,但一旦我从arrEqps删除对象,从arrCompEqp arrEqps的对象也被去除。为什么它不保留数据。
答
首先,如果您希望桌子平稳工作并能够显示大量数据,您必须将单元入队和出队。
这里有一些修正代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TeraGoAppDelegate *appDel = (TeraGoAppDelegate *)[[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
}
// Set up the cell...
cell.textLabel.text = [(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:countEqpIndex] objectForKey:@"EQP_NAME"];
if(![[(NSMutableDictionary *)[appDel.arrEqp objectAtIndex:indexPath.row] objectForKey:@"select"] isEqualToString:@"0"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
关于索引的问题 - 我不明白确切的问题。
通常,每个表格单元表示数据源数组中的一个项目。
什么是countEqpIndex
?
为什么你在这里也不使用indexPath.row
?
答
我认为,如果你使用的开关和选择部分作为案例,以便在各种情况下我只好把不同的阵列所以我只好将你的阵列的每个部分,这是我的意见我希望能够帮助您
关于正如已经说过的那样,每个部分应该有一个单独的数组。它可能是一组字典数组,当顶部数组代表段时,内部数组 - 单元格和字典将保存要在每个单元格中显示的数据(就像您现在这样做)。 – 2010-06-07 10:32:41
感谢迈克尔您的回复,我会继续提出您的建议。当我完成它时,我会让你知道。 – pankaj 2010-06-08 05:52:00