如何将标签放入表格单元格中?

发布于 2024-11-25 19:33:27 字数 591 浏览 2 评论 0原文

表格单元格

这是我想要制作的单元格,左侧是 cell.text,右侧部分是标签。 现在表格样式是

UITableViewStyleGrouped 当我尝试制作标签时,我编写了这些代码。

cell.textLabel.text = @"All";
UIView* view = cell.contentView;
UILabel* label1 = [[UILabel alloc] initWithFrame:cell.frame];
label1.textColor = [UIColor blackColor];
label1.textAlignment = UITextAlignmentCenter;
label1.text = @"%15";
[view addSubview:label1];
[label1 release];

但这不起作用,因为标签的单元格覆盖了一个又一个。当然有人可以帮助我用代码制作这种外观吗?

table cell

this is the cell i want to make the left side is cell.text and the right part is label.
Now the table style is

UITableViewStyleGrouped When i try to make the label i write these codes.

cell.textLabel.text = @"All";
UIView* view = cell.contentView;
UILabel* label1 = [[UILabel alloc] initWithFrame:cell.frame];
label1.textColor = [UIColor blackColor];
label1.textAlignment = UITextAlignmentCenter;
label1.text = @"%15";
[view addSubview:label1];
[label1 release];

But this doesnt work because cell of the label cover one and another.Can any one help me to make this kind of look with code of course.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

很快妥协 2024-12-02 19:33:27

您的代码中的问题似乎是 label1 框架。如下更改其框架。

CGRect lFrame = CGRectMake(cell.frame.width - 100, 0, 100, cell.frame.height);
UILabel* label1 = [[UILabel alloc] initWithFrame:lFrame];

使用现有样式:您使用的样式已预先定义。无需将自定义标签添加到单元格中。您可以通过将表格单元格的样式指定为 UITableViewCellStyleValue1 来实现此样式。

[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 ....

您可以更改单元格 textLabeldetailedTextLabel 的字体属性,例如样式、颜色和大小,以满足您的需求。

The problem in your code seems to be the label1's frame. Change the its frame like the following.

CGRect lFrame = CGRectMake(cell.frame.width - 100, 0, 100, cell.frame.height);
UILabel* label1 = [[UILabel alloc] initWithFrame:lFrame];

Using exisiting style: The style you are using is already predefined. No need to add your custom label to the cell. You can achieve this style by specifying table cell's style to UITableViewCellStyleValue1.

[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 ....

You can change the font properties like style, color and size of the cell's textLabel and detailedTextLabel to fit your needs.

可爱咩 2024-12-02 19:33:27

只需将您的 UITableviewCell 类型设置为 UITableViewCellStyleValue1 并执行如下操作。

cell.textLabel.text = @"All";
cell.detailTextLabel.text = @"%15";

您根本不需要在单元格中添加视图。此外,您可以在使用 UILabel 进行更改时随意更改 textLable 和 DetailTextLabel 属性。它们是 UILabel 本身。所以你可以用 UILabel 做任何你能做的事情。

希望这有帮助。

Just make your UITableviewCell type to UITableViewCellStyleValue1 and do like below.

cell.textLabel.text = @"All";
cell.detailTextLabel.text = @"%15";

You don't need to add view in the cell at all. Also, you can change textLable and DetailTextLabel properties whatever you want as you change with UILabel. They are UILabel itself. So you can do everything whatever you can do with UILabel.

Hope this help.

∝单色的世界 2024-12-02 19:33:27

如果预定义的样式能够满足您的需求,那就太好了。

否则,这样的方法将适用于您希望对布局有更多权力或单元格中有更多视图的情况:

- (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;
}

The pre-defined styles are great if they cover your needs.

Otherwise an approach like this one will work for cases where you want more power over the layout, or more views in the cell:

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