【问题标题】:IBOutlet's are nil when using custom tableviewcells in a storyboard在情节提要中使用自定义 tableviewcells 时,IBOutlet 为零
【发布时间】:2014-04-16 00:03:19
【问题描述】:

情节提要有一个带有一个原型单元格的表格视图,UITableView 有一个原型单元格,并且它已被配置为自定义 UITableViewCell 子类。

原型单元格正确连接到自定义子类,IBOutlets 配置正确,但由于某种原因,当我得到单元格时,我的所有自定义子视图都为零。

我还对其进行了配置,以使 customIdentifiers 相同。

【问题讨论】:

    标签: ios objective-c uitableview storyboard


    【解决方案1】:

    所以我面临的问题是一个奇怪的疏忽,当您在情节提要中识别重用标识符时,您不必调用

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

    在 TableView 上。如果你这样做,这实际上会破坏它打算做的功能。

    当弄乱自定义 UITableViewCells 时,只需将reuseIdentifiers 设置为通用的,我相信这将为您在幕后完成 registerClass。如果你自己做,那是行不通的。

    【讨论】:

    • 您永远不必为故事板制作的单元格调用它。如果你的单元格是用代码制作的,你应该只注册一个类,但是文档对此不是很清楚。
    • 是的,这就是我想做的 SO Q & A 以便其他人能够意识到这一点。这不是什么明显的东西。更重要的是它会导致事情无法正常工作,这很麻烦。
    【解决方案2】:

    我导入了自定义单元类并在 cellForRowAtIndexPath 方法中实现了我的代码,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 2016-01-22
      • 1970-01-01
      • 2015-06-02
      • 2012-03-08
      相关资源
      最近更新 更多