0

我想知道为什么这不起作用,请帮助我。这是我的代码

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

*)indexPath
{
    if (selectedIndexPath == indexPath) {
        selectedIndexPath = nil;
    } else {
        selectedIndexPath = indexPath;
    }
    [self.tableView deselectRowAtIndexPath : indexPath animated : NO];
    [tableView beginUpdates];
    [tableView endUpdates];
}
4

2 回答 2

0

确保保留您的selectedIndexPath,以免nil无意中设置为。

@interface YourClass

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@end

然后将变量引用为self.selectedIndexPath,你应该没问题。

但还有第二个警告。NSIndexPath是一个对象,因此运算符==将只比较指针地址。显然,这些总是不同的,因为一个是你的变量,另一个是方法的私有变量!你想要使用的是

if ([indexPath isEqualTo:selectedIndexPath]) // not tested

或者

if (indexPath.row == selectedIndexPath.row && 
    indexPath.section == selectedIndexPath.section)
于 2012-06-19T05:13:56.687 回答
0

NSIndexPath object consists of two parts they are row and section. The indexPathForRow:inSection: method creates an NSIndexPath object from row and section index numbers. Make sure your selectedIndexPath is created properly. Follow this apple reference for more details.

于 2012-06-19T05:21:14.957 回答