2014-10-20 129 views
1

我发现很多这个问题的答案,但我不了解如何管理我的代码。 当我滚动tableView内容时,单元格消失。我在单元格问题和答案中只有两个文本。 我试图使用的标识符(可重复使用),但这些代码不会改变任何东西......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; 
} 

UPDATE计算单元尺寸

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

我想你还没有计算高度的顶部。请为'heightForRowAtIndexPath:'添加代码。 – 2014-10-20 12:29:06

+0

@ Chinttu-RoxeN-Ramani:我已经使用heightForRowAtIndexPath方法更新了我的帖子。 – Zuhn 2014-10-20 12:36:36

+0

你能避免重用细胞吗? – gran33 2014-10-20 13:25:33

回答

1

我已经在过去有这个问题并且总是因为单元格高度不正确而造成的......您有两个单元格不同的实例,因此我们必须掌握该代码。

int y 

该值也关于我的一点点,因为它看起来像它设置为textLabel框架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); 
} 

现在,我们为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" 

在您的视图控制器

相关问题