TableView单元格在滚动中消失

我发现了很多这个问题的答案,但我不知道如何pipe理我的代码。 当我滚动tableView内容时,单元格消失。 我在单元格中只有两个文本问题和答案。 我试图使用一个标识符(可重用),但代码不会改变任何东西…

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

更新计算细胞大小

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

我过去遇到过这个问题,总是因为单元格的高度不正确。你有两个单元格不一样的情况,所以我们必须抓住该代码。

 int y 

这个值也是关于我的一点,因为它看起来像它将textLabel框架origin.y设置为-20 …如果你想在每个单元格中Y的不同的偏移量,但其余的单元格是相同的,那很好,但在这种情况下,我想我会为两种types的单元格build议一个不同的UITableViewCell子类…并更改该子类内的所有标签属性等。

所以…

为每种types的单元格创build一个UITableViewCell子类,然后调用它们,例如MYQuestionTableViewCell和MYAnswerTableViewCell

在单元格MYQuestionTableViewCell.h文件中

 #define TEXT_LABEL_WIDTH 285.0f #define TEXT_LABEL_QUESTION_FONT_SIZE 16.0f 

在那个单元格的MYQuestionTableViewCell.m文件里面做

 - (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); } 

现在我们的textLabel将会是任何单元格的高度…现在它只设置在一个地方,我们知道如果这个问题不正确,那么问题将出现在哪里。

在你的第二个MYAnswerTableViewCell子类中,你需要设置其他值

。H

 #define TEXT_LABEL_ANSWER_FONT_SIZE 14.0f 

.M

与其他单元格相同,但改变它的属性值

正如你在两个不同的单元格中使用不同的字体…它可能会更容易使用一个开关..但我会尽量保持它类似于你以前做的事情..这种加倍的代码是造成混乱的原因,但有时这是不可避免的。我们只是尽量保持它尽可能简单。

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

你不应该添加标签到一个单元格,除非你需要,他们提供了一些提供…现在你的cellForRow可以看起来像这样

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

你也需要

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

在你的视图控制器的顶部