1

カスタム UITableView セルがあり、特定のセルを選択して別の ViewController に移動しました。最初のビュー コントローラーに戻ったときに、セルの選択状態が表示されません。別のviewControllerから移動した後、セルの選択状態を変更するにはどうすればよいですか?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [timeSet count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   timeSettingCell *newCell = nil;
   newCell = [tableView dequeueReusableCellWithIdentifier:identifier];
   if(newCell == nil)
   {
    NSLog(@"newCell ===================");
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"timeSettingCell"   owner:self options:nil];
    newCell  = [ nibViews lastObject];
    }
    newCell.timeLabel.text=[timeSet objectAtIndex:indexPath.row];

    if (newCell.selected==YES) {
      newCell.highlighted=YES;
      newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
    }
    else {
      newCell.highlighted=NO;
      newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
    }
return newCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
value=[[time1 objectAtIndex:indexPath.row]integerValue];

SettingsViewController *settings=[[SettingsViewController alloc]initWithNibName:nil bundle:nil andCounterValue:value];
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
index=indexPath.row;
[settings release];
}
-(void)viewWillAppear:(BOOL)animated{
}

ありがとうございました

4

2 に答える 2

2

このように置くと、うまくいきます...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];
于 2012-04-05T09:08:11.873 に答える
1

シングルトン、ivar、または NSUserDefault として作成された NSArray を使用してセルの状態 (選択されているかどうかにかかわらず) を記憶し、その状態を で読み取る必要があると思いますcellForRowAtIndexPath

編集: サンプルコード

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do what you want

// This save the index of your selected cell on disk
[[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"selectedCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create your cell

// Here you check if the cell should be selected or not
if (indexPath.row == [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedCell"]) {
    newCell.highlighted=YES;
    newCell.timeImage.image=[UIImage imageNamed:@"radioSelected.png"];
} else {
    newCell.highlighted=NO;
    newCell.timeImage.image=[UIImage imageNamed:@"radioNotSelected.png"];
}

return aCell;
}

NSUserDefaultsデバイスにデータを保存し、後で取得するのに便利です。これは、アプリを閉じて再度開いた後でも、セルの状態を確認できることを意味します。

使用NSUserDefaultsするには、Settings.bundle をプロジェクトに追加する必要があります。NSUserDefaults クラス リファレンスをご覧ください。

于 2012-04-05T08:13:26.357 に答える