Ios TableView单元格在滚动时消失

Ios TableView单元格在滚动时消失,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,对于这个问题,我找到了很多答案,但我不知道如何管理我的代码。 当我滚动tableView内容时,单元格消失。我的手机里只有两条短信问答。 我尝试使用标识符(可重用),但代码没有改变任何东西 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UILabel *textLabel = nil; NSString* text = @"";

对于这个问题,我找到了很多答案,但我不知道如何管理我的代码。 当我滚动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 frame origin.y设置为-20。。。如果您希望每个单元格中Y的偏移量不同,但单元格的其余部分相同,这很好,但在本例中,我建议为这两种类型的单元格使用不同的UITableViewCell子类。。。并更改该子类中的所有标签属性等

所以

为每种类型的单元格创建一个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);
}
现在我们的文本标签将是任何高度的细胞是。。。现在它只设置在一个地方,如果它不正确,我们知道问题会在哪里

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

h

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;
}
除非你需要,否则你不应该在单元格中添加标签,它们附带了一些。。。现在你的牢房可以像这样了

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

在视图控制器的顶部

我认为您没有计算高度。请为
heightforrowatinexpath:
@Chinttu RoxeN Ramani添加代码:我已经用heightforrowatinexpath方法更新了我的帖子。你能避免重复使用单元格吗?
#import "MYQuestionTableViewCell.h"
#import "MYAnswerTableViewCell.h"