ios - TableView 单元格在滚动时消失

标签 ios objective-c xcode uitableview

我找到了很多关于这个问题的答案,但我不明白如何管理我的代码。 当我滚动 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 有不同的偏移量但单元格的其余部分相同很好,但在这种情况下,我想我建议为两种类型的单元格使用不同的 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"

在你的 View Controller 的顶部

关于ios - TableView 单元格在滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26465698/

相关文章:

ios - 使用 Swift 从 CSV 初始化 CoreData

swift - 如何快速响应 macOS 应用程序中的 "Delete"命令?

swift - 从 Xcode 按钮运行 Shell 脚本

iOS 11.2.1 Xcode 9.2 BoringSSL SSL_ERROR_ZERO_RETURN(6)

ios - 选择 UIButton 时,这个按钮人工制品是什么?

ios - 离线地址获取

objective-c - OS X 中跨进程通信的首选方法是什么?

ios - Sqlite 在 ios SDK 中显示 EXC_BAD_ACCESS

objective-c - swift 1.2 : Implement optional property from Objc protocol

iphone - 合并 iCloud 和核心数据数据库