自定义表格单元格显示为零

时间:2015-06-25 15:40:39

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

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

第一个原型单元格是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];
    }
}

1 个答案:

答案 0 :(得分:2)

Make sure you register both reuse identifiers with the tableview using - (void)registerClass:(Class nullable)cellClass forCellReuseIdentifier:(NSString * nonnull)identifier Or registerNib if you have a nib for the cell.