简体   繁体   中英

UITableView DataSource must return a cell from cellforRowAtIndexPath Error

I have two different tableViews in one view controller and I get an error message. The data source and delegate are set to the view controller. Am I doing something wrong in the tableview methods. I haven't dealt with more than one tableView in the same view before. Thanks

- (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;

}

Unless you have already called registerClass:forCellReuseIdentifier: or defined a prototype class in your nib or storyboard on your UITableView , you're going to get nil cells back from that dequeReusableCellWithIdentifier: call. You can register one if you like, or you can create a local instance when you have received nil , making sure to call UITableViewCell initWithStyle:reuseIdentifier: as your initialization method.

I'm not seeing any immediate problems in your code. I assume those cells are registered properly in either a prototype cell or by registering them in code. If not then that is probably the issue. I would put a breakpoint in your cellForRowAtIndexPath: and step through the process to make sure its even being called for one, and make sure that it is in fact returning a cell. Make sure that the cell is not nil in all of your cases.

Not enough information but two among several reasons:

(1) You've not registered the two custom cells before trying to deque them.

If this is the case, register them while overridng viewDidLoad like follows.

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

or

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

(2) The identifier names you're using in cellForRowAtIndexPath method do not match what you registered in viewDidLoad method.

Double check the names and I strongly recommend you to use defined constant name in order to get support from Xcode.

You don't alloc memory for cells. Try this code:

- (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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM