1

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

  • (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);
    }
    

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

4

1 回答 1

0

names是正在刷新的MutableArray 吗?如果是这样,那是因为您每次选择单元格时都会对其进行初始化。因此最终删除了先前设置的值。

  • 在委托之外初始化names数组。也许在 viewDidLoad 或者你初始化 TableView 的地方

  • 在 didSelect 中,将新对象添加到names数组中

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

  • 从中删除整个逻辑cellForRowAtIndexPath并将其添加到 viewDidLoad 或您初始化 TableView 的任何位置
于 2012-12-06T06:18:30.577 回答