【问题标题】:custom table cell displaying nil显示零的自定义表格单元格
【发布时间】:2015-06-25 15:40:38
【问题描述】:

我正在尝试在我的第二个表格视图控制器的自定义单元格对象中设置标题并显示一个按钮,但只有我的第一个动态原型单元格显示文本。

第一个原型单元是 UITableViewCell,第二个是自定义对象。在我的第一个视图控制器中,我实现了几乎完全相同的方法,并且效果很好。不确定我的错误可能出在哪里,我使用的模型中包含所有数据。我的故事板中有正确的单元格标识符,并且设置了从自定义单元格到 UIButton 的连接。

提前致谢。

方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    static NSString *QuestionSectionIdentifier = @"QuestionSectionCell";

    if (indexPath.section == 0)
    {
        UITableViewCell *cellQuestion = [tableView
                                         dequeueReusableCellWithIdentifier:QuestionSectionIdentifier];
        if (cellQuestion == nil) {
            cellQuestion = [[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleSubtitle
                            reuseIdentifier:QuestionSectionIdentifier];
        }
        if (indexPath.row == 0) {
            cellQuestion.textLabel.text = _headerQuestion.question;
            NSLog(@"question here %@", cellQuestion.textLabel.text);
        }

        return cellQuestion;
    }
    else
    {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        if (indexPath.section == 1)
        {
            if (indexPath.row == 0)
            {
                [cell.firstButton setTitle:_headerQuestion.questionAuthor.fullName forState:UIControlStateNormal];
                [cell.firstButton setTag:_headerQuestion.questionAuthor.userID];
                [cell.answerLabelField setHidden:YES];

                NSLog(@"section 1  %@", cell.firstButton.titleLabel.text);
            }
        }
        else  if (indexPath.section == 2)
        {
            Answer* answer_num =  [_headerQuestion.answers objectAtIndex:indexPath.row]; //object at index
            [cell.firstButton setTitle:answer_num.answerAuthor.fullName forState:UIControlStateNormal];
            [cell.firstButton setTag:answer_num.answerAuthor.userID];
            cell.answerLabelField.text = answer_num.answer;
        }
        return cell;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    if (section == 1)
    {
        return 1;
    }
    else
    {
        return [_headerQuestion.answers count];
    }
}

【问题讨论】:

  • 究竟显示“nil”是什么? cell? cell.firstButton?
  • 我试图在 cell.firstButton 中记录文本,但它显示:(null)。在我的视图控制器中只显示第 0 行第 0 行的单元格,但 MyTableViewCell 中没有一个单元格。
  • 你确定tableView(_:cellForRowAtIndexPath:) 被调用了吗?
  • yes -- 从我的日志中输出“第 1 节”
  • 可以查看您的项目吗?

标签: ios objective-c uitableview uibutton custom-cell


【解决方案1】:

确保您使用 tableview 注册两个重用标识符

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

如果你有单元格的笔尖,或者 registerNib。

【讨论】:

  • 哦,我怎么称呼这个?你也知道为什么我不用 registerClass 方法就能在我的第一个 View Controller 上成功获得完整显示吗?
  • @khajj 一个注册的好地方是 viewDidLoad 或者你初始化你的 UITableView 的地方
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多