简体   繁体   English

Swift自定义tableViewCell

[英]Swift custom tableViewCell

I have created a custom UITableViewCell with a label on the far left (respects the parent margins) and it displays correctly. 我创建了一个自定义UITableViewCell ,其最左边带有一个标签(尊重父边距),并且可以正确显示。 However, when I set an image using imageView.image (from the UITableViewCell ), the label does not move to the right and so the label and the image are on top of each other. 但是,当我使用imageView.image (来自UITableViewCell )设置图像时,标签不会向右移动,因此标签和图像位于彼此的顶部。 Any ideas how to make the label behave like the default label where it will move to the right to make way for the image? 有什么想法可以使标签表现得像默认标签一样,它将向右移动以为图像腾出空间吗?

图片与标签重叠

If you create the cell from storyboard make sure you have set the UITableViewCell style to Custom . 如果从情节提要板创建单元格,请确保已将UITableViewCell样式设置为Custom

Edit: 编辑:

I saw your comment above. 我在上面看到了您的评论。 If you don't wish to create a custom UITableViewCell class, you can still use default UITableViewCell with style set to Basic 如果您不想创建自定义UITableViewCell类,则仍可以使用样式设置为Basic默认UITableViewCell

So you can obtain its properties: 这样就可以获得其属性:

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

The other solution is mentioned as commented above: You have to subclass UITableViewCell , drag and drop 3 IBOutlet s and reference them in .h file. 如上所述,提到了另一个解决方案:您必须将UITableViewCell子类化,拖放3个IBOutlet ,然后在.h文件中引用它们。

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

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