在编辑模式下使用自定义 UILabels 分组 UITableView

发布于 2024-11-09 21:28:38 字数 241 浏览 2 评论 0原文

我有一个分组的 UITableView,用户可以在其中进入编辑模式并从表中删除行。表格的每个单元格都有两个 UILabels。当表格进入编辑模式时,自定义 UILabels 会向右推送并超出单元格的右边框。

如果我使用标准 cell.textLabel,标签会调整大小并保持在单元格的边框内。关于如何使用自定义 UILabels 执行此操作的想法?

I have a grouped UITableView where the user can enter editing mode and delete rows from the table. Each cell of the table has two UILabels. When the table enters into editing mode, the custom UILabels push to the right and go beyond the right border of the cell.

If I use the standard cell.textLabel, the label resizes and stays within the borders of the cell. Ideas about how to do this with the custom UILabels ?

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

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

发布评论

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

评论(1

我恋#小黄人 2024-11-16 21:28:38

您需要实现并使用这两个 UITableViewDelegate 方法:

– tableView:willBeginEditingRowAtIndexPath:
– tableView:didEndEditingRowAtIndexPath:

在 willBegin 中,将 UILabel 框架设置为较小的宽度,并在 didEndEditing 上将宽度设置为正常大小。

例如,如果您的 UILabel 被推离边界 50 像素,则在您的方法中执行以下操作:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    CGRect newFrame = thisCell.someUILabel.frame;

    newFrame.size.width -= 50;

    thisCell.someUILabel.frame = newFrame;
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    CGRect newFrame = thisCell.someUILabel.frame;

    newFrame.size.width += 50;

    thisCell.someUILabel.frame = newFrame;
}

You need to implement and use these two UITableViewDelegate methods:

– tableView:willBeginEditingRowAtIndexPath:
– tableView:didEndEditingRowAtIndexPath:

In willBegin, set your UILabel frame to have a smaller width and set the width to normal size on didEndEditing.

For example, if your UILabel is getting pushed 50 pixels out of the boundary, in your methods you do:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    CGRect newFrame = thisCell.someUILabel.frame;

    newFrame.size.width -= 50;

    thisCell.someUILabel.frame = newFrame;
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    CGRect newFrame = thisCell.someUILabel.frame;

    newFrame.size.width += 50;

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