滚动时,存储在数组中的表视图单元索引正在丢失

时间:2012-12-06 06:10:17

标签: xcode tableview

我正在研究最近几天的桌面视图。但是,现在我被卡住了。实际上,我所做的是我想将单元格索引存储到一个可变数组中。我做了这个部分。但是,当我滚动表视图时,存储在数组中的数据将被刷新,并且我存储的索引将从该可变数组中删除。这是我到目前为止所做的代码......

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(tableView == table1){     static NSString * CellIdentifier = @“Cell”;

    NSString *CellId = [NSString stringWithFormat:@"Cell %d",indexPath.row];
    
    CellIdentifier = CellId;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    
    {                
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    }
    NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
    NSString *loadname1 = [defaults1 objectForKey:@"name1saved"];
    
    NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
    NSString *loadname2 = [defaults2 objectForKey:@"name2saved"];
    
    NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
    NSString *loadname3 = [defaults3 objectForKey:@"name3saved"];
    
    NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
    NSString *loadname4 = [defaults4 objectForKey:@"name4saved"];
    
    NSUserDefaults *defaults5 = [NSUserDefaults standardUserDefaults];
    NSString *loadname5 = [defaults5 objectForKey:@"name5saved"];
    
    NSUserDefaults *defaults6 = [NSUserDefaults standardUserDefaults];
    NSString *loadname6 = [defaults6 objectForKey:@"name6saved"];
    
    NSUserDefaults *defaults7 = [NSUserDefaults standardUserDefaults];
    NSString *loadname7 = [defaults7 objectForKey:@"name7saved"];
    
    NSUserDefaults *defaults8 = [NSUserDefaults standardUserDefaults];
    NSString *loadname8 = [defaults8 objectForKey:@"name8saved"];
    
    NSUserDefaults *defaults9 = [NSUserDefaults standardUserDefaults];
    NSString *loadname9 = [defaults9 objectForKey:@"name9saved"];
    
    NSUserDefaults *defaults10 = [NSUserDefaults standardUserDefaults];
    NSString *loadname10 = [defaults10 objectForKey:@"name10saved"];
    
    NSUserDefaults *defaults11 = [NSUserDefaults standardUserDefaults];
    NSString *loadname11 = [defaults11 objectForKey:@"name11saved"];
    
    NSUserDefaults *defaults12 = [NSUserDefaults standardUserDefaults];
    NSString *loadname12 = [defaults12 objectForKey:@"name12saved"];
    
    NSUserDefaults *defaults13 = [NSUserDefaults standardUserDefaults];
    NSString *loadname13 = [defaults13 objectForKey:@"name13saved"];
    
    NSUserDefaults *defaults14 = [NSUserDefaults standardUserDefaults];
    NSString *loadname14 = [defaults14 objectForKey:@"name14saved"];
    
    NSUserDefaults *defaults15 = [NSUserDefaults standardUserDefaults];
    NSString *loadname15 = [defaults15 objectForKey:@"name15saved"];
    
    NSUserDefaults *defaults16 = [NSUserDefaults standardUserDefaults];
    NSString *loadname16 = [defaults16 objectForKey:@"name16saved"];
    
    NSUserDefaults *defaults17 = [NSUserDefaults standardUserDefaults];
    NSString *loadname17 = [defaults17 objectForKey:@"name17saved"];
    
    NSUserDefaults *defaults18 = [NSUserDefaults standardUserDefaults];
    NSString *loadname18 = [defaults18 objectForKey:@"name18saved"];
    
    NSUserDefaults *defaults19 = [NSUserDefaults standardUserDefaults];
    NSString *loadname19 = [defaults19 objectForKey:@"name19saved"];
    
    NSUserDefaults *defaults20 = [NSUserDefaults standardUserDefaults];
    NSString *loadname20 = [defaults20 objectForKey:@"name20saved"];
    
    mTFArrayOfNames = [[NSMutableArray alloc]init ];
    
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname1] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname2] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname3] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname4] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname5] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname6] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname7] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname8] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname9] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname10] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname11] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname12] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname13] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname14] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname15] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname16] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname17] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname18] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname19] atIndex:[mTFArrayOfNames count]] ;
    [mTFArrayOfNames insertObject:[NSString stringWithFormat:@"%@", loadname20] atIndex:[mTFArrayOfNames count]] ;
    
    NSLog(@"%@", mTFArrayOfNames);
    
    if (indexPath.row == 0) {
    
        cell.textLabel.text = loadname1;
    }
    
    if (indexPath.row == 1) {
        cell.textLabel.text = loadname2;
    }
    
    if (indexPath.row == 2) {
        cell.textLabel.text = loadname3;
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = loadname4;
    }
    if (indexPath.row == 4) {
    
        cell.textLabel.text = loadname5;
    }
    
    if (indexPath.row == 5) {
        cell.textLabel.text = loadname6;
    }
    
    if (indexPath.row == 6) {
        cell.textLabel.text = loadname7;
    }
    if (indexPath.row == 7) {
        cell.textLabel.text = loadname8;
    }
    
    if (indexPath.row == 8) {
    
        cell.textLabel.text = loadname9;
    }
    
    if (indexPath.row == 9) {
        cell.textLabel.text = loadname10;
    }
    
    if (indexPath.row == 10) {
        cell.textLabel.text = loadname11;
    }
    if (indexPath.row == 11) {
        cell.textLabel.text = loadname12;
    }
    if (indexPath.row == 12) {
    
        cell.textLabel.text = loadname13;
    }
    
    if (indexPath.row == 13) {
        cell.textLabel.text = loadname14;
    }
    
    if (indexPath.row == 14) {
        cell.textLabel.text = loadname15;
    }
    if (indexPath.row == 15) {
        cell.textLabel.text = loadname16;
    }
    if (indexPath.row == 16) {
    
        cell.textLabel.text = loadname17;
    }
    
    if (indexPath.row == 17) {
        cell.textLabel.text = loadname18;
    }
    
    if (indexPath.row == 18) {
        cell.textLabel.text = loadname19;
    }
    if (indexPath.row == 19) {
        cell.textLabel.text = loadname20;
    }
    
    
    
    cell.textLabel.font = [UIFont fontWithName:@"Bauhaus 93" size:18];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor grayColor];
    [cell.textLabel setTextAlignment:UITextAlignmentCenter];
    
    return cell;
    

    } }

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(tableView == table1){     UITableViewCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
        [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
        selectedCell.backgroundColor =[UIColor blueColor];
    
    } else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    
        [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    
        selectedCell.backgroundColor =[UIColor clearColor];
    
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    names = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < mTFArrayOfNames.count; i++)
    {
        if ([self.table1 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]].accessoryType == UITableViewCellAccessoryCheckmark) 
        {
            [names addObject:[mTFArrayOfNames objectAtIndex:i]];
    
      }
        NSLog(@"name1 is  %@", names);
    }
    

} } 请帮我。谢谢你提前......

1 个答案:

答案 0 :(得分:0)

是否正在刷新names MutableArray? 如果是这样,那是因为每次选择单元格时都要初始化它。因此最终删除了先前设定的值。

  • 在委托中初始化names数组。也许在viewDidLoad或你初始化TableView

  • 的地方
  • 在didSelect中,在names数组中添加新对象

此外,在loadnameX中创建所有cellForRowAtIndexPath并添加到新初始化的mTFArrayOfNames是错误的逻辑。

  • cellForRowAtIndexPath删除整个逻辑,并将其添加到viewDidLoad或初始化TableView的位置