2016-06-22 123 views
0

我已经创建了一个自定义UITableViewCell与最左边的标签(尊重父页边距),它显示正确。但是,当我使用imageView.image(来自UITableViewCell)设置图像时,标签不会向右移动,因此标签和图像彼此重叠。任何想法如何使标签的行为像默认的标签,它会移动到右边为图像开路?Swift自定义tableViewCell

image overlap with label

+0

当图像存在时,您需要更新标签的框架(或自动布局约束)。 –

+0

你有关于如何做到这一点的代码示例? –

+0

如果你正在设置框架,'label.frame = CGRect(...)' –

回答

0

如果您创建的细胞从故事板确保你已经在UITableViewCell样式设置为Custom

编辑:

我看到你上面的评论。 如果你不希望创建一个自定义UITableViewCell类,你仍然可以使用默认UITableViewCell与样式设置为Basic

这样你就可以得到它的属性:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = @"Your text"; 
    cell.imageView.image = [UIImage imageNamed:@"Your image name"]; 
    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame: CGRectZero]; 
    cell.accessoryView = mySwitch; 

    // Define the state of the switch because the cell will get recycled as you scroll. 
    BOOL isOn; 
    [mySwitch setOn:isOn]; 

    return cell; 
} 

另一种解决方案被提及作为评论以上:您必须继承UITableViewCell,将其拖放到3 IBOutlet s并在.h文件中引用它们。

+0

我已经做到了,但想重用uitableviewcell的imageView。但看起来不起作用。 –

相关问题