表视图,选定单元格,不接受用户单击



我有一个类似于首选项页面的uitableview。我使用NSDefaults保存用户选择,加载时重新加载这些默认值,并将表视图单元格设置为显示为选中。

我用这种方法

   - (UITableViewCell *)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//if this cell should look as if it was selected as a preference {
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6];
    cell.selectedBackgroundView = selectionColor;
    cell.selected = true;
}
else{
    cell.selected = false;
}
return cell;
}

现在,如果用户取消选择一个单元格,我在这里处理

  - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

我遇到的问题是,如果我加载应用程序,然后正确设置之前选择的单元格,它们就永远不会接受点击事件!!因此用户永远不能取消选择它们。

它可能对您有所帮助。

试试这个方法

 - (UITableViewCell *)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexpath.row == selectedrow) {
    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(200/255.0) green:(200/255.0) blue:(200/255.0) alpha:0.6];
    cell.selectedBackgroundView = selectionColor;
    cell.selected = true;
}
else{
    cell.selected = false;cell.selectedBackgroundView = nil;
}
return cell;
}

如果你需要任何细节,请告诉我。

最新更新