UITableViewCell textLabel 在选择时改变位置

UITableViewCell textLabel changing position on selection

我已经使用代码创建了一个 UITableView 和一个 UITableViewCell 的子类,它显示得非常好,直到我 select 单元格。

单元格的textLabel有时会右移或不移

selection

之前问题的图像表示

select离子后问题的图像表示

相关代码

class MyCell: UITableViewCell {

override func layoutSubviews() {
    super.layoutSubviews()
    // here I fixed the image at a specific point
    self.imageView?.bounds = CGRectMake(0, 0, 100, 125)
    self.imageView!.frame = CGRectMake(0, 0, 100, 125)
    self.imageView?.contentMode = .ScaleAspectFill

} }
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: "FeedCell")

    self.configureCell(cell, atIndexPath: indexPath)

    return cell
}

func configureCell(cell: MyCell, atIndexPath indexPath: NSIndexPath) {
    let item = self.items[indexPath.row] as MWFeedItem 
    cell.textLabel?.text = item.title 
    cell.textLabel?.font = UIFont.systemFontOfSize(12.0)
    cell.textLabel?.numberOfLines = 0
}

你真的应该在你的子类中定义你自己的图像视图和文本标签(具有不同的名称)并应用约束以根据单元格的大小对它们进行布局,然后在控制器中设置行高以便东西显示得当。

即使在您的第一张图片中,图片的大小也不正确,因此您的单元和控制器之间的配置不匹配。并且您当前使用的属性在大小和位置方面应被视为私有属性,因为您不拥有设置这些视图功能的实现。