ios - 如何在单击按钮时将 ImageView 添加到表格单元格中?

标签 ios swift xcode uitableview uiimageview

我正在尝试做这样的事情:

enter image description here

当用户单击“添加页面”时,其下方会显示一个新的分组。现在,我决定使用 TableView 单元格来实现这一目标。在遵循各种教程并查找类似的问答之后,我能够使用 UILabel 在按钮单击上添加单元格,并且单元格高度根据内容是动态的,但现在我试图弄清楚如何添加 ImageView 并将按钮放置在单元格内。

我创建了一个自定义单元类:

class PageCell : UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCellSelectionStyle.none

        setupViews()
    }

    ...
    ... // other random code here


    let imgView : UIImageView = {
        let imgview = UIImageView()
        imgview.frame = CGRect(x: 100, y: 150, width: 150, height: 140)
        imgview.tintColor = UIColor(red: 0.73, green: 0.2, blue: 0.3, alpha: 1.0)
        imgview.translatesAutoresizingMaskIntoConstraints = false
        return imgview
    }()

    func setupViews() {
        addSubview(pageLabel) // the label that I got working
        addSubview(imgView) // can't get this working
        ...
        // constraint info here
    }
}

回到我的 TableViewController:

class TakePhotosVC: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(PageCell.self, forCellReuseIdentifier: "cellID")
    }

    // return the actual view for the cell
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let pagecell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! PageCell
        // set more stuff here
    }

    ... // more code
} 

我的问题是,我试图获取一个显示 ImageView 所在位置的框,用户可以单击该框来加载图片。我不确定如何做到这一点并放置所有相关按钮(垃圾桶、X 等)

任何帮助将不胜感激。

编辑

好吧,我正在尝试关注 this教程,但我无法让它正常工作。在我的原型(prototype)单元中,我看到了这个:

enter image description here

但是结果是这样的:

enter image description here

我让UIImageView有一个背景,这样我就可以看到它。我对 UIImageView 有两个约束:宽度 = 240,高度 = 128,对页面标签有两个约束:宽度 = 240,高度 = 21。两个问题:为什么我的元素没有正确放置,即使我已将其正确放置在 Storyboard中?为什么单元格高度没有动态调整大小以适应元素?

我在 TakePhotosVCviewDidLoad 方法中有这两行,但它似乎没有做任何事情。

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200

如果相关,我在运行模拟器时会收到此警告。

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

编辑2

我让它工作了。对于在我之后阅读本文的任何可怜的人,您必须单击“约束”窗口编辑器中的那些粉红色虚线,然后单击“添加 X 约束”以使 ImageView 居中和填充。

最佳答案

My issue is that I am trying to get a box showing where the ImageView is that the user can click on to load in a picture. I am unsure how to do that and place all the relevant buttons as well (Trash, X, etc.)

我不确定我的理解是否正确。您有一个带有 UIImageView 的单元格。但是您想要显示一个框来直观地指示用户应触摸的位置来添加 UIImage(?)

为什么不简单地在 UIImageView 顶部添加一个具有完全相同帧大小的 UIButton,然后在触摸时触发操作来添加图像,一旦成功添加图像,您就可以将 UIButton 设置为隐藏。

如果用户使用垃圾按钮删除图像,您只需通过隐藏 = NO 再次显示 UIButton。

其他解决方案: 使用自定义颜色向 UIImageView 添加边框,并添加 UITapGestureRecognizer 来触发操作。 (确保将 UIImageView 设置为 userInteractionEnabled = YES;

当没有图像集时,您还可以使用自定义设计将占位符图像添加到 UIImageView。

关于ios - 如何在单击按钮时将 ImageView 添加到表格单元格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42003767/

相关文章:

ios - AVAudioEngine 的语音识别在录制后阻止声音

ios Build 只能运行 24 小时

swift - 为什么 SomeStruct() is AnyObject 返回 true?

json - 匹配行选择传递数据

ios - Xcode 8 iOS 模拟器没有在我的 Swift 3 项目中提示位置服务授权

ios - xcode-阻止音频文件相互播放

ios - 将自己从 Bonjour 搜索结果中排除的方法

ios - instantiateViewControllerWithIdentifier 似乎调用了 viewdidload

ios - 适用于 iOS 的 Firebase ML 套件 : "Unable to convert data to a string using the detected encoding" error when using local model

objective-c - 识别用户何时关闭窗口(单击关闭按钮时)