2011-03-21 73 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