0

我有一个表格视图,其中包含 AppDelegate 中的项目列表。表格视图允许用户选择他们想要的项目,并且在选择之后项目旁边有一个复选标记。接下来,用户可以返回上一个屏幕,此选择会更新显示给用户的字符串值。该值也存储在 AppDelegate 中。一切正常,但如果用户选择返回到用户可以更改其选择的屏幕,我希望自动选择此值。

这是我的 SetSizeViewController.m 中的 cellFroRowAtIndexPath 函数

我无法选择存储在 appDelegate.selectedSize 中的值。例如,此值可能类似于“Meter”。

- (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 回答