gpt4 book ai didi

ios - 表格单元格行中的 objective-c 不同的uilabel

转载 作者:行者123 更新时间:2023-12-01 16:15:01 25 4
gpt4 key购买 nike

我正在尝试创建一个在每一行中包含一些uilabels的表。 uilabel的编号基于我的数组,如果我的数组有一个字符串,那么它只会是一个标签,如果我的数组有2个字符串,那么该行将有两个标签。

第一排:

enter image description here

第二行:

enter image description here

这是将字符串放入已经存在的uilabel中的代码。如果我有更多的字符串,如何使单元格自动生成更多的uilabel?

- (void)updateCell:(NSString *)text1 label2:(NSString *)text2 label3:(NSString *)text3{
self.testLabel1.text = text1;
self.testLabel2.text = text2;
self.testLabel3.text = text3;
}

- (void)createLabel{
//create uilabels based on the size of array?
}

如果这不可行,可能有其他替代方法吗?任何建议深表感谢。

最佳答案

所有这些都应该在cellForRowAtIndexPath中完成,您基本上应该将数组用于该行索引,然后以编程方式创建uilabel,这是一些代码,虽然行不通,但是逻辑应该没问题

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

NSArray *stringLabels = [self.labels objectAtIndex:indexPath.row]; // im not sure how you are getting the array

for (NSString *str in stringLabels) {
UILabel *newLabel = [[UILabel alloc]init];
newlabel.SetFrame = CGrectMake(Set to your desired layout);
newlabel.text = str;
// uilabel formatting

[cell addSubview:newLabel]
}

}

这种方法非常灵活,因为它允许您在每个单元格中具有任意数量的标签。

关于ios - 表格单元格行中的 objective-c 不同的uilabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545232/

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