向每个单元格添加额外的 UIlabel

发布于 2024-11-25 23:59:06 字数 636 浏览 1 评论 0原文

我试图在每个单元格中添加一个额外的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-12-02 23:59:06

看看教程“UITableView – 将子视图添加到单元格的内容视图"。

尝试类似的操作

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 320, 65);
    CGRect Label1Frame = CGRectMake(17,5,250,18);  

    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    [lblTemp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
    lblTemp.tag = 1;
    lblTemp.backgroundColor=[UIColor clearColor];
    lblTemp.numberOfLines=0;
    [cell.contentView addSubview:lblTemp];      
    return cell;  // this I forgot  
}

在 cellForRowAtIndexPath 中

{
    if(cell == nil)
            cell = [self getCellContentView:CellIdentifier];

    UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];  
    lblTemp1.text =[nameArray objectAtIndex:value+indexPath.row];  
        return cell;
}

Have a look at the tutorial "UITableView – Adding subviews to a cell’s content view".

Try something like this

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 320, 65);
    CGRect Label1Frame = CGRectMake(17,5,250,18);  

    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    [lblTemp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
    lblTemp.tag = 1;
    lblTemp.backgroundColor=[UIColor clearColor];
    lblTemp.numberOfLines=0;
    [cell.contentView addSubview:lblTemp];      
    return cell;  // this I forgot  
}

in cellForRowAtIndexPath

{
    if(cell == nil)
            cell = [self getCellContentView:CellIdentifier];

    UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];  
    lblTemp1.text =[nameArray objectAtIndex:value+indexPath.row];  
        return cell;
}
二智少女 2024-12-02 23:59:06

为了在所有单元格上使用相同的标签,您可能只实例化一个单元格并对所有单元格使用 cellReUseIdentifier。因此,我建议您创建不同的标签作为类的属性,并将每个 property-label 分配给每个单元格。

// For example - 

if (indexPath.row ==0 )
  // assign [cell.contentView addSubview: self.label1];

if (indexPath.row ==1 )
  // assign [cell.contentView addSubview: self.label2];

对于问题的第二部分,请完整发布您的 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 each property-label to each cell.

// For example - 

if (indexPath.row ==0 )
  // assign [cell.contentView addSubview: self.label1];

if (indexPath.row ==1 )
  // assign [cell.contentView addSubview: self.label2];

And for the second part of the question, please post your cellForRowAtIndexPath fully - there could be an error in that.

情绪失控 2024-12-02 23:59:06

首先检查是否有任何预定义样式适合您。

如果您需要比他们提供的更多的灵活性,您可以尝试这样的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [self makeCell: CellIdentifier];
    }

    MyData *data =  [self.data objectAtIndex:indexPath.row];

    UILabel *lbl1 = (UILabel *)[cell viewWithTag:1];
    UILabel *lbl2 = (UILabel *)[cell viewWithTag:2];

    lbl1.text = data.text;
    lbl2.text = data.auxText;    

    return cell;
}


- (UITableViewCell *)makeLensListCell: (NSString *)identifier
{
    CGRect lbl1Frame = CGRectMake(10, 0, 140, 25);
    CGRect lbl2Frame = CGRectMake(10, 150, 140, 25);

    UILabel *lbl;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];

    // Label with tag 1.
    lbl = [[UILabel alloc] initWithFrame:lbl1Frame];
    lbl.tag = 1;
    [cell.contentView addSubview:lbl];
    [lbl release];

    // Label with tag 2.
    lbl = [[UILabel alloc] initWithFrame:lbl2Frame];
    lbl.tag = 2;
    lbl.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:lbl];
    [lbl release];

    // Add as many labels and other views as you like

    return cell;
}

这种方法还允许图像和其他视图。

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:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [self makeCell: CellIdentifier];
    }

    MyData *data =  [self.data objectAtIndex:indexPath.row];

    UILabel *lbl1 = (UILabel *)[cell viewWithTag:1];
    UILabel *lbl2 = (UILabel *)[cell viewWithTag:2];

    lbl1.text = data.text;
    lbl2.text = data.auxText;    

    return cell;
}


- (UITableViewCell *)makeLensListCell: (NSString *)identifier
{
    CGRect lbl1Frame = CGRectMake(10, 0, 140, 25);
    CGRect lbl2Frame = CGRectMake(10, 150, 140, 25);

    UILabel *lbl;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];

    // Label with tag 1.
    lbl = [[UILabel alloc] initWithFrame:lbl1Frame];
    lbl.tag = 1;
    [cell.contentView addSubview:lbl];
    [lbl release];

    // Label with tag 2.
    lbl = [[UILabel alloc] initWithFrame:lbl2Frame];
    lbl.tag = 2;
    lbl.textColor = [UIColor lightGrayColor];
    [cell.contentView addSubview:lbl];
    [lbl release];

    // Add as many labels and other views as you like

    return cell;
}

This approach also allows images and other views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文