2014-10-20 128 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" 

在您的視圖控制器