2010-06-07 178 views
0

嗨,我有一個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的對象也被去除。爲什麼它不保留數據。

回答

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

+0

關於正如已經說過的那樣,每個部分應該有一個單獨的數組。它可能是一組字典數組,當頂部數組代表段時,內部數組 - 單元格和字典將保存要在每個單元格中顯示的數據(就像您現在這樣做)。 – 2010-06-07 10:32:41

+0

感謝邁克爾您的回覆,我會繼續提出您的建議。當我完成它時,我會讓你知道。 – pankaj 2010-06-08 05:52:00

0

我認爲,如果你使用的開關和選擇部分作爲案例,以便在各種情況下我只好把不同的陣列所以我只好將你的陣列的每個部分,這是我的意見我希望能夠幫助您