Uitableview无法保存数据

时间:2010-06-07 09:55:30

标签: iphone

嗨我有一个uitableview,我正在显示一些约200行的数据。数据将显示在几个部分中,这些部分将在运行时确定。我能够获得部分但我无法以正确的顺序显示特定部分的数据。我在数组中以字典的形式存在数据。

我的代码:

- (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中删除了对象,arrEqp的arrEqps对象也会被删除。为什么不保留数据。

2 个答案:

答案 0 :(得分:0)

首先,如果您希望表格顺利运行并且能够显示大量数据,您必须将单元格入队并出列队列。

以下是对您的代码的一些更正:

- (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

答案 1 :(得分:0)

我认为如果你使用Switch并选择部分作为案例,那么在每种情况下你必须放置不同的数组,所以你必须为每个部分划分你的数组,这是我的意见,我希望能帮到你

相关问题