custom table cell displaying nil

325 views Asked by At

I'm trying to set the title and display a button in my custom cell object in my second table view controller, but only my first dynamic prototype cell is displaying text.

The first prototype cell is UITableViewCell and the second is the custom object. In my first view controller I implement almost the same exact method and it works completely fine. Not sure where my error could be, the models I use have all their data in them. I have the correct cell identifier in my storyboard, and the connection from custom cell to the UIButton is set.

Thanks in advance.

Method:

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

There are 1 answers

2
bonnie lapin On

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.