2011-06-19 17 views
5

Iphoneはかなり新しいIphoneです。これは私のプロジェクトの最後のモジュールです。chekmarkで複数の国を選択するための完全なコードです。 基本的に私の質問は、選択した国を保存し、このビューに戻ったときにチェックマークを再度表示する方法です。あなたのデータはあなたが使用することができそれほど大きくない場合 NSuserdefaultsに選択されたチェクリストの行を保存します

マイ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]; 
} 

マイ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; 
} 

おかげ

答えて

0

..ここに私を助けてくださいNSUserDefaults。データ保存する

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]を。

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

[userDefaults synchronize];

データを取得するには、次の

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]を。あなたはCountry array、当初と同じNSMutableArrayarray.coutを作成する必要が私によると

-

0

それを解決するために非常に多くの方法があります。

[「選択」@ userDefaults objectForKey] arraydefault valueを格納し、 をdidSelectRowAtIndexPathに保存すると、をUITableViewCellAccessoryCheckmarkにしたがって変更する必要があります。

その後、あなたはNSUserDefaultsNSMutableArrayを保存する必要があります -

Link - FOR SAVE ARRAY IN NSUserDefaults

関連する問題