0

AppDelegate からのアイテムのリストを含むテーブル ビューがあります。テーブル ビューでは、ユーザーが必要な項目を選択できます。選択が完了すると、項目の横にチェックマークが表示されます。次に、ユーザーは前の画面に戻ることができます。この選択により、ユーザーに表示される文字列値が更新されます。この値は AppDelegate にも格納されます。すべてがうまく機能しますが、ユーザーが選択を変更できる画面に戻ることを選択した場合、この値が自動的に選択されるようにしたいと思います。

これが私の SetSizeViewController.m の cellFroRowAtIndexPath 関数です。

appDelegate.selectedSize に格納されている値を選択するのに問題があります。たとえば、この値は「メーター」のようなものです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 self.checkedIndexPath = indexPath;

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *result = nil;

static NSString *CellIdentifier = @"CellIdentifier";

result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];
SetSize *setSize = (SetSize *)[appDelegate.setSizes objectAtIndex:indexPath.row];

appDelegate.selectedSize = setSize.name;

appDelegate.selectedSizeCheckedIndexValue = (NSInteger *)indexPath.row;

[tableView reloadData];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

TabAndSplitAppAppDelegate *appDelegate = (TabAndSplitAppAppDelegate *)[[UIApplication sharedApplication] delegate];

if(appDelegate.selectedSize.length > 0)
{
    if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {

        [cell setSelected:YES animated:YES];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        [cell setSelected:NO];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
}
4

1 に答える 1

1

自分自身に電話することは避けdidSelectRowAtIndexPath:てください。とにかく、電話してみてください[result setSelected:YES animated:YES]

また、このセルが選択されるセルであるかどうかをどのように確認していますか? 間違っている場合は修正してください。しかし、これを使用するとif(appDelegate.selectedSize) { ...、すべてのセルが選択コードに対して真になるように思えます。

編集:

以下を削除[result setSelected:YES animated:YES]cellForRowAtIndexPathて追加します。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == mySelectedRow) { //mySelectedRow is the index value you stored previously.

        cell.selected = YES; //or [cell setSelected:YES animated:YES] if you want it to be animated.
    }
}

編集2:

念のため、最初に if 関数を次のように調整しますwillDisplayCell:

if ((int)indexPath.row == (int)appDelegate.selectedSizeCheckedIndexValue) {

    [cell setSelected:YES animated:YES]; //or animated:NO depends on what you need.
    [self performSelector:@selector(unselectCellAtIndexPath:) withObject:indexPath afterDelay:1.0]; //add a delay to the deselection. you can change the delay time to whatever suits you.
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}
else {

    [cell setSelected:NO];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

-(void)unselectCellAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

次にdidSelectRowAtIndexPath:、セルを選択した後、内で呼び出します[tableView reloadData]

于 2012-08-21T17:37:27.967 に答える