UITableView DataSource必须从cellforRowAtIndexPath错误返回一个单元格

时间:2014-10-07 19:50:29

标签: ios objective-c xcode uitableview

我在一个视图控制器中有两个不同的tableView,我收到一条错误消息。数据源和委托设置为视图控制器。我在tableview方法中做错了什么。我以前没有在同一个视图中处理过多个tableView。感谢

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (tableView == self.postsTableView) {
    return 1;
}
else if (tableView == self.eventsTableView){
return 1;
}
return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.postsTableView) {
    return 1;
}
else if (tableView == self.eventsTableView){
    return 1;
}
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView isEqual: self.postsTableView]) {
    profilePagePostCell *cellOne = (profilePagePostCell *) [tableView dequeueReusableCellWithIdentifier:@"profilePostCell"];
    cellOne.postLabel.text = [NSString stringWithFormat:@"Hi"];
    return cellOne;
}

if ([tableView isEqual: self.eventsTableView]) {
    profileEventCell *cellTwo = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:@"profileEventCell"];
    cellTwo.eventLabel.text = [NSString stringWithFormat:@"The big One"];
    return cellTwo;
}

profileEventCell *cell = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:@"profileEventCell"];
return cell;

}

4 个答案:

答案 0 :(得分:2)

除非您已在registerClass:forCellReuseIdentifier:上的笔尖或故事板中调用了UITableView或定义了原型类,否则您将从nil获取dequeReusableCellWithIdentifier:个单元格1}}打电话。您可以根据需要注册一个,也可以在收到nil后创建本地实例,并确保将UITableViewCell initWithStyle:reuseIdentifier:作为初始化方法。

答案 1 :(得分:1)

我的代码中没有立即出现任何问题。我假设这些单元格在原型单元格中正确注册或通过在代码中注册它们。如果没有那么那可能就是问题所在。我会在你的cellForRowAtIndexPath中放一个断点:并逐步完成整个过程,以确保它被调用一个,并确保它实际上是返回一个单元格。确保在所有情况下单元格都不为零。

答案 2 :(得分:1)

信息不足,但有两个原因:

(1)在尝试去除它们之前,您没有注册这两个自定义单元格。

如果是这种情况,请在覆盖viewDidLoad时注册它们,如下所示。

[self.postsTableView registerClass:[profilePagePostCell class] forCellReuseIdentifier:@"profilePostCell"];

[self.postsTableView registerNib:@"YOUR_NIB_NAME" forCellReuseIdentifier:@"profilePostCell"]

(2)您在cellForRowAtIndexPath方法中使用的标识符名称与您在viewDidLoad方法中注册的名称不匹配。

仔细检查名称,我强烈建议您使用已定义的常量名称以获得Xcode的支持。

答案 3 :(得分:-1)

您不为单元格分配内存。试试这段代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath{

    static NSString *cellIdentifierPosts = @"cellIdentifierPosts"
    static NSString *cellIdentifierEvents = @"cellIdentifierEvents"

    if ([tableView isEqual: self.postsTableView]) {
        profilePagePostCell *cellOne = (profilePagePostCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierPosts];
        if (!cellOne)
            cellOne = [[profilePagePostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierPosts];
        cellOne.postLabel.text = [NSString stringWithFormat:@"Hi"];
        return cellOne;
    }

    else if ([tableView isEqual: self.eventsTableView]) {
        profileEventCell *cellTwo = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierEvents];
        if (!cellTwo)
            cellTwo = [[profileEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierEvents];
        cellTwo.eventLabel.text = [NSString stringWithFormat:@"The big One"];
        return cellTwo;
    }
    else{
        profileEventCell *cell = (profileEventCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifierEvents];
        if (!cell)
            cell = [[profileEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifierEvents];
        cell.eventLabel.text = [NSString stringWithFormat:@"The default One"];
        return cell;
    }
}