Search code examples
iosobjective-cuitableviewcustom-cell

Custom UITabeViewCell not working


I need help in custom UITableViewCell

I am having two custom cells created in which I am checking my Question type using DTO. If its is MCQ then load QuizMCQTableViewCell or if it is MRQ then load QuizMRQTableViewCell.

Here is my code below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.currentQuestionDTO type] isEqualToString:@"MCQ"])
    {
        QuizMCQTableViewCell *customcell = [tableView dequeueReusableCellWithIdentifier:optionCellIdentifier];
        if (customcell == nil)
        {
            customcell = [[[QuizMCQTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:optionCellIdentifier] autorelease];

        }else
        {
            UIImageView* MRQIV=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]];
            customcell.checkBoxImgV.image = MRQIV.image;
            customcell.cellTextLbl.text = @"Hello";
        }
        return customcell;
    }

    if ([[self.currentQuestionDTO type] isEqualToString:@"MRQ"])
    {
        QuizMRQTableViewCell *customcell = [tableView dequeueReusableCellWithIdentifier:optionCellIdentifier];
        if (customcell == nil)
        {
            customcell = [[[QuizMRQTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:optionCellIdentifier] autorelease];

        }else
        {
            UIImageView* MRQIV=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio.png"]];
            customcell.radioBoxImgV.image = MRQIV.image;
            customcell.cellTextLbl.text = @"Hi";

        }
        return customcell;
    }
    return nil;
}

What I am facing problem is that, When My tableView loaded then very first Question type is MCQ so it goes for first cell, but didn't display anything, as I wrote the code in else loop of if (customcell == nil) So this is my first issue, why not text is set to hello. But it work when I write my code in if loop.

and my second issue is, When my code for type MCQ is in if (customcell == nil) When I load second question type MRQ my app get crash saying following error

-[QuizMCQTableViewCell radioBoxImgV]: unrecognized selector sent to instance

what I am doing wrong. Please help.

Thanks in advance..


Solution

  • The first problem Question type is MCQ so it goes for first cell, but didn't display anything. Is that if you don't have the cells added in a UITableView inside a xib/sotryboard. The dequeCell will return nil so the else block won't be executed, this will result in empty display.

    Second, unrecognized selector means that the property/method 'radioBoxImgV' doesn't exist on the object of QuizMCQTableViewCell. This is because you are using the same cell identifier for both cell types so the table view will return a cell of type QuizMCQTableViewCell. Crate two identifiers, one for first cell and one for second cell type.

    Third, if you are using ARC, please remove the autorelease form the alloc/init.

    Forth, NEVER EVER EVER RETURN nil from cellForRowAtIndexPath