1

i'm working on table view for last few days. but, now i'm stuck. actually, what m i doin' is that i i want to store cell indexes into a mutable array. i'd done this part. but, as i scroll the table view, the data stored in the array gets refreshed and the indexes that i stored gets removed from that mutable array. here's the code what i have done so far....

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

} } Please help me. thankx in advance....

4

1 に答える 1

0

Is it the names MutableArray that is getting refreshed ? If so, it is because you are initializing it everytime a cell is selected. Thus ending up in deleting the previously set value.

  • Initialize the names array outsite the delegate. maybe in viewDidLoad or where ever you intialize the TableView

  • In didSelect, add the new object in the names array

Also, it is bad logic to create all the loadnameX in the cellForRowAtIndexPath and add to newly initialized mTFArrayOfNames.

  • Remove this entire logic from cellForRowAtIndexPath and add it to viewDidLoad or where ever you intialize the TableView
于 2012-12-06T06:18:30.577 に答える