IOS——如何在 UITableViewCell 中移动 UILabel?

标签 ios objective-c uitableview

我正在尝试创建一个(不是很)自定义 UITableView,在单元格的左侧有一个按钮,在按钮的右侧有文本。

我尝试计算放置作为单元格一部分的 UILabel 的位置,但更改框架没有任何效果(它甚至似乎没有被计算——全为零)。

我的问题是:何时计算标签的帧大小? (这样更改才会有效果)。

这是正确的做法吗?

下面的代码片段。 首先是来自 cell init(调用以响应 dequeueReusable ... 是的,'buttonColor'、'onTapSel'、'buttonFrame' 和 'buttonColor' 是“成员变量(文件静态全局变量)

- (id)initWithStyle: (UITableViewCellStyle)style
    reuseIdentifier: (NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (nil == buttonColor)
    {   buttonColor = kDefaultCellButtonColor; }

    if (nil == onTapSel)
    {   onTapSel = @selector(defaultCellButtonTapped:); }

    if (self)
    {
        EGCellButton * cellButton = (EGCellButton *)[[EGCellButton alloc ] initWithFrame:buttonFrame ];
        cellButton.backgroundColor = buttonColor;
        [cellButton  setTitle:buttonLabel forState:UIControlStateNormal];
        [cellButton addTarget:self action:(onTapSel) forControlEvents: UIControlEventTouchUpInside];
        [self setCellButton:cellButton];

        float xx = buttonFrame.origin.x + buttonFrame.size.width + 5;
        CGRect frame = self.textLabel.frame;
        frame.origin.x   += xx;
        self.textLabel.frame = frame;
        [self addSubview:cellButton];
    }
    return self;
}

现在来自 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"EGButtonTableCellID";
    EGButtonTableCell * cell = (EGButtonTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.cellButton.indexPath = indexPath;

    // Configure the cell...

    cell.textLabel.text = @"abcdefghijklmnopqrstuvwxyz";

    float xx = cell.cellButton.frame.origin.x + cell.cellButton.frame.size.width + 5;
    CGRect frame = cell.textLabel.frame;
    frame.origin.x   += xx;
    cell.textLabel.frame = frame;

    return cell;
}

结果是按钮出现在单元格的左侧(如预期的那样),但我在标签中看到的第一个字母是“g”,因此前 6 个字符隐藏在按钮后面(不是预期的) .

当我转储帧中的值时,两种方法中除 origin.x 之外的所有值均为零。

这应该可行,是吗?不?为什么不? ETC。 非常感谢大家!

:bp:

最佳答案

在自定义 UITableViewCell 的 layoutSubviews 方法中为 UILabel 和 UIButton 设置框架

请参阅有关该主题的 Apple 文档:

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

关于IOS——如何在 UITableViewCell 中移动 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107608/

相关文章:

iphone - 多个 requestWithGraphPath Facebook iOS

ios - 将 uibutton 聚焦在 uicollectionviewCell 内

ios - 在 EditActionsForRow 中返回一个空的 UITableViewRowAction 数组会中断在表格单元格上滑动

ios - "Allow Push Notifications"对话框被关闭一次后如何取回?

ios - 如何让我所有的自动布局约束自行更新? (例如,当用户更新他们的系统大小/动态类型时)?

ios - uitableview重置删除按钮

ios - MapView 注释中的较长字幕(快速)

iphone - 自定义 UITableViewCell 中的自定义 UIView

objective-c - 在 UITableViewCell 中动态查找 UIWebView 高度

iphone - 具有分配属性的属性委托(delegate)的现有实例变量委托(delegate)必须是 unsafe_unretained