【问题标题】:UIImageView on TableViewCell missing its constraints on clicking cellTableViewCell 上的 UIImageView 缺少单击单元格的约束
【发布时间】:2017-02-02 04:46:05
【问题描述】:

要求:

我正在做一个sideBar navigation,在里面使用UITableViewUITableViewCell。在UITableViewCell里面,有一个UIImageView和一个UILabel。我希望UIImageView具有相同的height 和 width,我已经给了它限制。

问题:

但是在单击每个单元格后,图像视图的大小会发生变化(缺少约束)并与标签重叠。

我只在情节提要中做了所有约束,我在 swift 3 中做 请帮帮我。

代码工作:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
     cell.label.text = data[indexPath.row]
     let iconName = images[indexPath.row]
    //print("\(iconName)")
     let image = UIImage(named: iconName)
   // print("The loaded image: \(image)")
     cell.imageViews.image = image
    
         return cell
}

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
   tableView.deselectRow(at: indexPath, animated: true)
}

屏幕截图

【问题讨论】:

  • 如果实现了显示你的 didSelectForRow?
  • cell.imageView.maskToBound = true ,试试这个
  • @Himanshu func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) }
  • 为什么要取消选择 didselect 方法上的单元格。显示您的原型单元约束。
  • 即使我避免取消选择,结果也是一样的--@the_dahiya_boy

标签: ios swift uitableview uiimageview tableviewcell


【解决方案1】:
        for viw in cell.contentView.subviews {
        if ((viw as? UIImageView) != nil) {
            viw.removeFromSuperview()
        }else if ((viw as? UILabel) != nil) {
            viw.removeFromSuperview()
        }
    }

在单元格创建后使用此代码 [从第二行开始]。试试看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2021-06-13
    • 1970-01-01
    相关资源
    最近更新 更多