【问题标题】:iOS 5.1 dequeueReusableCellWithIdentifier throws NSInvalidArgument exceptioniOS 5.1 dequeueReusableCellWithIdentifier 抛出 NSInvalidArgument 异常
【发布时间】:2012-12-18 18:27:25
【问题描述】:

我有一个在 iOS6.1 上运行良好的应用。今天我意识到我可能也应该尝试让它与 iOS5 兼容。我尝试在 iOS 5 模拟器上运行它,并在我的 dequeCell 方法调用中抛出异常。我不知道为什么它在 iOS6 上运行良好。其他人遇到过这个问题吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = //throws exception here
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                     forIndexPath:indexPath];
    cell.accessoryView = nil;
    cell.backgroundColor = [UIColor colorWithRed:.97 
                                           green:.97 
                                            blue:.97 
                                           alpha:1];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    ...
    return cell;
}

-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized 
 selector sent to instance 0x8a3a000 -*** Terminating app due to uncaught 
 exception 'NSInvalidArgumentException', reason: '-[UITableView 
 dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector 
 sent to instance 0x8a3a000'

【问题讨论】:

    标签: objective-c uitableview ios5.1


    【解决方案1】:

    据我所知,dequeueReusableCellWithIdentifier:forIndexPath: 方法仅在 iOS 6.0 中添加。

    更好地使用dequeueReusableCellWithIdentifier:

    【讨论】:

    • 完美。此外,我所有的按钮也都停止工作了。它们显示正常,但似乎它们与代码无关,你能想到什么?如果可以的话,我想避免发布另一个愚蠢的问题。
    • 你的单元格内有按钮吗? '他们没有被捆绑',你的意思是你的按钮是 IBOutlets 并且没有被分配?它们是如何创建的...
    【解决方案2】:

    @Chase,我今天在尝试使我的应用与 iOS 5.1 兼容时也遇到了同样的问题。

    我让按钮工作的一种方法是使用手势识别器并在您实现的 tapDetected 委托方法中,查询您想要的按钮并直接在代码中调用它。
    在 ViewDidLoad 中注册识别器

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.delegate = self;
    [self.view addGestureRecognizer:tapRecognizer];
    
    
        - (void)tapDetected:(UIGestureRecognizer*)tap{
    NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    if ( 6 == [[versionCompatibility objectAtIndex:0] intValue] ) {
        [self.view endEditing:YES];
    } else {
        if (CGRectContainsPoint(self.createSessionButton.frame, [tap locationInView:self.view])) {
            [self.view endEditing:YES];
            [self createNewSession:nil];
        }
       }
    

    }

    存在 iOS 检查的原因是因为在 ios 6 中这不是问题。很抱歉格式错误,在这里发帖还是新手。

    【讨论】:

    • 除了这些说明之外,您能否发布代码示例(看起来不错,顺便说一句)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2011-05-30
    相关资源
    最近更新 更多