1

I found a lot of answer for this question but I don't anderstand how to manage my code. When I scroll the tableView content, the cell disappear. I just have two texts in the cell question and answer. I've tried to use an identifier (reusable) but the code doesn't change anything...

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *textLabel = nil;
NSString* text = @"";


//static NSString *CellIdentifier = @"Cell";
NSString *CellIdentifier = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];



UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int y = -20;
float titleFontSize = 14.0f;
if(indexPath.row == 0) {
    y = 0;
    titleFontSize = 16.0f;
}

if(cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];


textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[textLabel setMinimumFontSize:14.0f];
[textLabel setNumberOfLines:0];
[textLabel setFont:[UIFont systemFontOfSize:14.0f]];
[textLabel setTag:1];
[textLabel setTextColor:[UIColor colorWithRed:57/255.0 green:55/255.0 blue:64/255.0 alpha:1.0]];

// Titre
if(indexPath.row == 0) {
    text = question;
    [cell setBackgroundColor:[UIColor colorWithRed:220/255.0 green:224/255.0 blue:241/255.0 alpha:1.0]];//[UIColor colorWithRed:.945f green:.921f blue:.78f alpha:1]];
    [textLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:titleFontSize]];
}

// Contenu
else {
    text = answer;

}




CGSize constraint = CGSizeMake(285, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:titleFontSize] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
[textLabel setText:text];
[textLabel setFrame:CGRectMake(25, y, 275, MAX(size.height + 60, 44.0f))];
[textLabel setBackgroundColor:[UIColor clearColor]];

[cell addSubview:textLabel];

return cell;
}

UPDATE CALCULATING CELL SIZE

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

float titleFontSize = 14.0f;
float offSet = 0;
NSString *text = @"";

if(indexPath.row == 0) {
    text = question;
    titleFontSize = 16.f;
    offSet = 10;
}

else
    text = answer;


CGSize constraint = CGSizeMake(285, 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:titleFontSize] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

CGFloat height = MAX(size.height + 40, 44.0f);

return height + offSet;
}
3
  • 1
    i think you have not calculate height. please add code for heightForRowAtIndexPath:. Oct 20, 2014 at 12:29
  • @Chinttu-RoxeN-Ramani : I have updated my post with the heightForRowAtIndexPath method.
    – Zuhn
    Oct 20, 2014 at 12:36
  • Can u avoid from the Reuse cells?
    – gran33
    Oct 20, 2014 at 13:25

1 Answer 1

1

I've had this problem in the past and it's always due to the cells height being incorrect... You've got quite a few instances where your two cells are not the same, so we are going to have to get a grip on that code.

int y 

this value is also concerning me a little as it looks like it sets the textLabel frame origin.y to -20 ... if you want a different offset for Y in each cell but the rest of the cell is the same, that's fine, but in this case I think I'd suggest a different UITableViewCell subclass for the two types of cell... And change all the label properties etc inside that subclass..

So...

Make a UITableViewCell subclass for each type of cell, call them whatever you like... for example MYQuestionTableViewCell and MYAnswerTableViewCell

inside the cell MYQuestionTableViewCell .h file

#define TEXT_LABEL_WIDTH 285.0f
#define TEXT_LABEL_QUESTION_FONT_SIZE 16.0f

inside that cell MYQuestionTableViewCell .m file do

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.backgroundColor = [UIColor redColor];        
        self.clipsToBounds = YES; // just to make sure we're calculating the height correctly

        // set up your textLabel etc. in here
        self.textLabel.textColor = [UIColor whiteColor];
        self.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: TEXT_LABEL_QUESTION_FONT_SIZE]];

    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0.0f, 0.0f, TEXT_LABEL_WIDTH, self.contentView.frame.size.height);
}

Now our textLabel will be whatever the height of the cell is... Now it's set in only one place, we know where the issue will be if it's not correct.

In your second MYAnswerTableViewCell subclass you'll need the other values set

.h

#define TEXT_LABEL_ANSWER_FONT_SIZE 14.0f

.m

same as the other cell but changing for it's property values

As you are also using different fonts in the two different cells... it might be easier to use a switch.. but I'll try to keep it similar to what you were doing before.. this sort of doubling up of code is the cause of the confusion, but sometimes it's unavoidable.. We'll just try to keep it as simple as we can.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

CGFloat offSet = 0.0;
NSString *text = @"";
UIFont *fontUsed = nil;

if(indexPath.row == 0) {
    fontUsed = [textLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size: TEXT_LABEL_QUESTION_FONT_SIZE]];
    text = question;
    offSet = 10.0f;
}
else
{
    fontUsed =[UIFont systemFontOfSize:TEXT_LABEL_ANSWER_FONT_SIZE];
    text = answer;
}
    NSLog (@"TEXT: %@",text); // checking if the text is set...

CGSize constraint = CGSizeMake(TEXT_LABEL_WIDTH, HUGE_VALF); // personally I prefer HUGE_VALF for that

CGSize size = [text sizeWithFont:fontUsed constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

CGFloat height = MAX(size.height + 40.0f, 44.0f); // 40.0f for padding?

return height + offSet;
}

You shouldn't be adding labels to a cell unless you need to, they come with some provided... now your cellForRow can look like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *QuestionCellIdentifier = @"QuestionCell";
    static NSString *AnswerCellIdentifier = @"AnswerCell";

   if (indexPath.row == 0)
   {
            MYQuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier];

            if (!cell)
                cell = [[MYQuestionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:QuestionCellIdentifier];

      cell.textLabel.text = question;

      return cell;

   }

    MYAnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: AnswerCellIdentifier];     

    if (!cell)
       cell = [[MYAnswerTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AnswerCellIdentifier];

      cell.textLabel.text = answer;

      return cell;
}

You'll also need

#import "MYQuestionTableViewCell.h"
#import "MYAnswerTableViewCell.h"

at the top of your view controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.