IBOutlet's are nil when using custom tableviewcells in a storyboard

允我心安 提交于 2019-12-21 09:35:32

问题


The storyboard has a tableview with one prototype cell, the UITableView has a prototype cell and it has been configured to be the custom UITableViewCell sublclass.

The prototype cell is hooked up to the custom sublcass correctly, the IBOutlets are configured correctly, but for some reason when I get the cell it ends up all my custom subviews are nil.

I've also configured it so that the customIdentifiers are the same.


回答1:


So the problem that I was facing was a weird oversight, when you identify a reuseIdentifier in the storyboard, you don't have to call

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier;

on the TableView. If you do, this will actually BREAK the functionality it's intended to do.

When messing with custom UITableViewCells, just set up the reuseIdentifiers to be in common and that will do the registerClass behind the scenes for you I believe. If you do it yourself, it won't work.




回答2:


I imported the Custom Cell class and implement my codes in cellForRowAtIndexPath method like below:

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


    static NSString *CellIdentifier = @"FixtureCustomCell";

    FixtureCustomCell *cell = (FixtureCustomCell *)[fixtureTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {

        NSArray *topLabelObject = [[NSBundle mainBundle] loadNibNamed:@"FixtureCustomCell" owner:self options:nil];

        for (id currentObject in topLabelObject)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (FixtureCustomCell *) currentObject;
                break;
            }
        }
    }
    Fixture *currentFixture = [[xmlParser fixtures] objectAtIndex:indexPath.row];
    cell.dateLabel.text = currentFixture.date;
    NSLog(@"%@",currentFixture.date);
    cell.venueLabel.text=currentFixture.venue;
    NSLog(@"%@",currentFixture.venue);
    cell.firstTeamNameLabel.text=currentFixture.firstTeam;
    NSLog(@"%@",currentFixture.firstTeam);
    cell.secondTeamNameLabel.text=currentFixture.secondTeam;

    cell.timeLabel.text=currentFixture.time;
    cell.matchType.text=currentFixture.matchType;
    cell.specialMatchLabel.text=currentFixture.specialMatch;

    //    cell.firstTeamLogoImageView.image=currentFixture.firstTeamImage;
    //    cell.secondTeamLogoImageView.image=currentFixture.secondTeamImage;
    imageQueuefirstTeamLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(imageQueuefirstTeamLogo, ^
                   {
                       UIImage *imageTVGuideLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFixture firstTeamImage]]]];
                       dispatch_async(dispatch_get_main_queue(), ^
                                      {
                                          cell.firstTeamLogoImageView.image = imageTVGuideLogo;
                                          [cell setNeedsLayout];
                                      });
                   });

    imageQueuesecondTeamLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(imageQueuesecondTeamLogo, ^
                   {
                       UIImage *imageTVGuideLogo2 = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFixture secondTeamImage]]]];
                       dispatch_async(dispatch_get_main_queue(), ^
                                      {
                                          cell.secondTeamLogoImageView.image = imageTVGuideLogo2;
                                          [cell setNeedsLayout];
                                      });
                   });

    return cell;
    // Set up the cell...




}


来源:https://stackoverflow.com/questions/23096664/iboutlets-are-nil-when-using-custom-tableviewcells-in-a-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!