向每个单元格添加额外的 UIlabel
我试图在每个单元格中添加一个额外的 UILabel
(UITableView
) 我在
中成功使用了此代码 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法
这是我的代码
//add another text
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(250,80,50,20.0)];
test.text = [NSString stringWithFormat: @"test"];
test.backgroundColor = [UIColor clearColor];
test.font=[UIFont fontWithName:@"AppleGothic" size:20];
[cell addSubview:test];
但是我认为如果我这样做,我不能向每个单元格添加不同的文本,而是,它最终会在所有单元格中显示相同的文本。谁能告诉我该怎么做?
哦,另一个问题是,如果我这样做,除了第一个单元格之外的每个单元格中都会显示“测试”。
I'm trying to add an extra UILabel
into each cell(UITableView
)
I'v succeeded with this code in(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method
Here's my code
//add another text
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(250,80,50,20.0)];
test.text = [NSString stringWithFormat: @"test"];
test.backgroundColor = [UIColor clearColor];
test.font=[UIFont fontWithName:@"AppleGothic" size:20];
[cell addSubview:test];
However I figured that if I do this, I can't add different text to each cell, instead, it ends up with the same text in all cells. Can anyone tell me how to do that?
Oh and another problem was that if I do this, "test" is displayed in every cell except for the first one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看教程“UITableView – 将子视图添加到单元格的内容视图"。
尝试类似的操作
在 cellForRowAtIndexPath 中
Have a look at the tutorial "UITableView – Adding subviews to a cell’s content view".
Try something like this
in cellForRowAtIndexPath
为了在所有单元格上使用相同的标签,您可能只实例化一个单元格并对所有单元格使用
cellReUseIdentifier
。因此,我建议您创建不同的标签作为类的属性,并将每个property-label
分配给每个单元格。对于问题的第二部分,请完整发布您的
cellForRowAtIndexPath
- 其中可能存在错误。For having the same Label on all cells, you are probably instantiating just one cell and use the
cellReUseIdentifier
for all cells. So, I suggest that you create different labels as properties of the class, and assign eachproperty-label
to each cell.And for the second part of the question, please post your
cellForRowAtIndexPath
fully - there could be an error in that.首先检查是否有任何预定义样式适合您。
如果您需要比他们提供的更多的灵活性,您可以尝试这样的方法:
这种方法还允许图像和其他视图。
First check if any of the predefined styles work for you.
If you need more flexibility than what they offer, you can try something like this:
This approach also allows images and other views.