2011-06-19 10 views
5

Jestem całkiem nowy dla Iphone i to jest mój ostatni moduł w moim projekcie. Poniższy kod działa idealnie do wyboru wielu krajów za pomocą chekmark. Zasadniczo moje pytanie brzmi, jak zapisać te wybrane kraje i ponownie pokazać znaczniki wyboru po powrocie do tego widoku. proszę mi tu pomóc ..zapisz wybrane wiersze listy w liscie NSuserdefaults

Moja metoda didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

    if([selectedCell accessoryType] == UITableViewCellAccessoryNone) 
    { 
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]]; 
    } 
    else 
    { 
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone]; 
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]]; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    [tableView reloadData]; 
} 

Moja metoda cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    HolidayAppDelegate *delegatObj = (HolidayAppDelegate *)[UIApplication sharedApplication].delegate; 

    static NSString *CellIdentifier = @"CustomCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    cell.textLabel.text=[delegatObj.allcountryarray objectAtIndex:indexPath.row]; 

    [cell setAccessoryType:UITableViewCellAccessoryNone]; 

    for (int i = 0; i < selectedIndexes.count; i++) 
    { 
     NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue]; 
     if (num == indexPath.row) { 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
      break; 
     } 
    } 

    return cell; 
} 

Dzięki

Odpowiedz

0

Jeśli dane nie jest tak duża, można użyć NSUserDefaults.

Aby zapisać dane:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject: selectedIndexes forKey: @ "selection"];

[synchronizacja użytkownikówDomyślnych];

Aby dane:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults objectForKey: @ "selekcja"]

0

Jest tak wiele sposobów, aby go rozwiązać -

Według mnie trzeba stworzyć NSMutableArray i array.cout samo jak Country array i początkowo Przechowujesz default value w swoim array, w musisz zmienić zapisany default value zgodnie z UITableViewCellAccessoryCheckmark.

A potem trzeba przechowywać NSMutableArray w NSUserDefaults -

Link - FOR SAVE ARRAY IN NSUserDefaults

Powiązane problemy