2011-06-19 16 views
5

Iam es bastante nuevo para Iphone y este es mi último módulo en mi proyecto ... El siguiente código funciona perfectamente para la selección de varios países con chekmark. Básicamente mi pregunta es cómo guardar esos países seleccionados y mostrar las marcas de verificación nuevamente cuando regrese a esta vista. por favor ayúdame aquí ..guardar las filas seleccionadas de cheklist en NSuserdefaults

Mi método 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]; 
} 

Mi método 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; 
} 

Gracias

Respuesta

0

Si los datos no es tan grande que puede utilizar NSUserDefaults.

para guardar los datos:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

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

[userDefaults synchronize];

para obtener datos:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults objectForKey: @ "selección"]

0

Hay tantas manera de resolverlo -

Según yo lo que tiene que crear un NSMutableArray y array.cout mismo que su Country array e inicialmente usted almacena el default value en su array, en didSelectRowAtIndexPath tiene que cambiar el default value almacenado de acuerdo con UITableViewCellAccessoryCheckmark.

Y después de que usted tiene que almacenar el NSMutableArray en el NSUserDefaults -

Link - FOR SAVE ARRAY IN NSUserDefaults

Cuestiones relacionadas