UIDataView选择未回发

时间:2011-04-28 05:37:13

标签: iphone

我是iPhone编程新手,很难找到基本问题的答案。我在UIView上创建了一行UITableview,并希望单击鼠标单击以重定向到另一个UITableView屏幕以选择其中一个可用值。一切顺利,直到选择视图上的选择,但点击后退按钮后,新的选定值不会反映出来。我正在使用此代码

在我的通话视图

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // open a alert with an OK and cancel button
    UIViewController *nextViewController = nil;
    nextViewController = [[CategorySelectionView alloc] initWithStyle:UITableViewStyleGrouped];
    ((CategorySelectionView *)nextViewController).category = parentCategory;
    [self.navigationController pushViewController:nextViewController animated:YES];
    [nextViewController release];//break;

}

On selection View

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

    // If there was a previous selection, unset the accessory view for its cell.
    NSManagedObject *currentCategory = category;

    if (currentCategory != nil) {
        NSInteger index = [categoryTypes indexOfObject:currentCategory];
        NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
        checkedCell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Set the checkmark accessory for the selected row.
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];    

    category = [categoryTypes objectAtIndex:indexPath.row];

}

这是Apple开发人员食谱代码的摘录,但不适用于我。真的很感激任何帮助。此外,如果我可以重定向到使用tableView屏幕作为选择器的示例,将同样有帮助。期待您的帮助

1 个答案:

答案 0 :(得分:0)

如果您想在didSelectRowAtIndexPath

中取消选择下面的单元格使用
[tableView deselectRowAtIndexPath:indexPath animated:NO];    

所以你的代码必须如下所示。

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

    // If there was a previous selection, unset the accessory view for its cell.
    NSManagedObject *currentCategory = category;

    if (currentCategory != nil) {
        NSInteger index = [categoryTypes indexOfObject:currentCategory];
        NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
        checkedCell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Set the checkmark accessory for the selected row.
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];    
    category = [categoryTypes objectAtIndex:indexPath.row];

    //Add it to deselect the selected cell
    [tableView deselectRowAtIndexPath:indexPath animated:NO];    
}

<强>编辑:

当您单击后退按钮时,获取该事件并更新数据(由您的tableview显示),然后在reloadData实例上调用UITableView方法。 要获取更新值,可以使用UITableView的reloadData方法。

使用如下

[tableView reloadData];
相关问题