2011-03-21 46 views
0

我試圖在數組中存儲分組的UITableView(例如所有單個單元格),以便我可以保留用戶點擊了哪些單元格(並將檢查附件添加到該單元格中)。每當我嘗試恢復細胞時,我最後都會顯示最近的細胞(即剛剛出現在屏幕底部的細胞出現在頂部滾動回來時。) 我無法弄清楚熱正確地做,所以任何幫助是非常歡迎 感謝在數組中存儲分組的UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
MyManager *dataStore = (MyManager*)[MyManager sharedManager]; 

NSNumber *dif = [[NSNumber alloc] initWithInt:-1]; 

if (indexPath.section == 0) { 
    dif = [NSNumber numberWithInt:0]; 
} else if (indexPath.section == 1) { 
    dif = [NSNumber numberWithInt:3]; 
} else if (indexPath.section == 2) { 
    dif = [NSNumber numberWithInt:5]; 
} else if (indexPath.section == 3) { 
    dif = [NSNumber numberWithInt:9]; 
} 


if ([dataStore.masterArray objectAtIndex:(indexPath.row+[dif intValue])][email protected]"0") { 
//array is initalised with 16 strings of @"0", just to fill it 


static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";  
NSArray *listData =[self.data objectForKey: 
        [self.sortedKeys objectAtIndex:[indexPath section]]];  
UITableViewCell * cell = [tableView 
          dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 

if(cell == nil) {   
    cell = [[[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:SimpleTableIdentifier] autorelease]; 
} 
NSUInteger Row = [indexPath row]; 
cell.textLabel.text = [listData objectAtIndex:Row]; 
cell.accessoryType = UITableViewCellAccessoryNone; 
    [dataStore.masterArray replaceObjectAtIndex:(indexPath.row + [dif intValue]) withObject:cell]; 

    cell.textLabel.text = [listData objectAtIndex:Row]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    return cell; 
    } 
else { 
UITableViewCell *cell = [dataStore.masterArray objectAtIndex:(indexPath.row+[dif intValue])]; 


    return cell; 
} 

} 

我會認爲此代碼應檢查是否存在索引處的單元格,如果沒有那麼做,並顯示(在此工作正常)。如果單元格確實存在,將其從數組中拉出並顯示出來。

回答

0

問題是單元不是一次性創建的,它們在必要時被創建和銷燬。出於視野,他們可能會被銷燬以挽救記憶。不是將單元格本身存儲在數組中,而是將布爾值存儲在數組中。然後,當爲特定行創建單元格時,使用數組外的bool值來確定是否應該檢查它。

+0

哇,當我的解決方案如此簡單時,我一直都在浪費,謝謝你,作爲一個魅力 – Darc 2011-03-21 01:34:34

+0

hhaha,好吧,它有助於獲得全新的視角。很高興我能幫上忙。 – drewag 2011-03-21 01:55:06