在UITableView的单元格中选择特定的字符串值

时间:2012-08-21 16:04:40

标签: ios uitableview

我有一个表格视图,其中包含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;
    }
}
}

1 个答案:

答案 0 :(得分: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:

首先调整willDisplayCell:中的if函数,以防万一:

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]