gpt4 book ai didi

ios - 在 cellForRowIndexPath 中的每个自定义单元格中创建标签

转载 作者:行者123 更新时间:2023-11-28 21:54:58 25 4
gpt4 key购买 nike

我正在尝试在我的每个 customCell 中创建一个标签。在 cellForRowIndexPath 中,我有:

编辑:

static NSString *checkInTableIdendifier = @"ChatCell";

cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:checkInTableIdendifier];

if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:[self.savedMsgs objectAtIndex:indexPath.row] attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:12], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)]; //2
label.attributedText = title; //3
[cell addSubview:label]; //4

由于第 2 行,标签将全部在同一点相互重叠。我如何表示根据每个相应单元格生成的标签的原点?

最佳答案

您用于生成单元格的方法“dequeueReusableCellWithIdentifier”可以返回之前已经使用过的单元格。当您在获取单元格后添加标签等时,您是在已经存在的标签之上添加标签。

处理单元格重用的最简单方法是创建一个 UITableViewCell 子类和一个关联的 xib。在创建此 ViewController 并且可以访问 tableView 之后(通常是 viewDidLoad),将单元格的 Nib 注册到 tableView。然后,当您将单元格出队时,您只需在已经存在的标签上设置文本值。

除此之外,UITableView 有一个更新的更好的方法来使单元格出列,该方法采用索引路径并始终返回一个单元格,因此您无需对其进行 nil 检查。如果您不需要支持 iOS 6,我会切换到那个。

关于ios - 在 cellForRowIndexPath 中的每个自定义单元格中创建标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com