繁体   English   中英

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

[英]Grouped UITableView with custom UILabels in Editing Mode

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

如果我使用标准cell.textLabel ,则 label 会调整大小并保持在单元格的边界内。 关于如何使用自定义UILabels做到这一点的想法?

你需要实现和使用这两个 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM