IBOutlet's are nil when using custom tableview

2020-07-08 07:26发布

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.

2条回答
我只想做你的唯一
2楼-- · 2020-07-08 08:06

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.

查看更多
一夜七次
3楼-- · 2020-07-08 08:23

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...




}
查看更多
登录 后发表回答