ios - 如何以编程方式在 UITableViewCell 中添加 UIView?

标签 ios swift xcode uitableview uiviewcontroller

我一直在创建一个应用程序,其 View 与 AppStore (iOS 11) - {Today} View 相同。

只是想知道我如何处理该 View 。然而,我认为方法是创建一个带有 UITableViewDataSource 和 -Delegate 扩展名的 UIViewController,我可以在我的 ViewController 中获取行数和数据。在 dequeReusableCell 中,我创建了一个 UITableViewCell 类,我在其中以编程方式创建了一个 UIView,但这不起作用。

class MyTableViewCell: UITableViewCell {

let cellView:UIView = {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    view.layer.cornerRadius = 15
    view.backgroundColor = UIColor.clear
    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowOpacity = 1
    view.layer.shadowOffset = CGSize.zero
    view.layer.shadowRadius = 5
    return view
}()
}

在上面的类片段中,无法在主视图中插入我的 UIView。

没有添加 View 的方法(很明显),因为它在 UIViewController 而不是 UITableViewCell 下。 所以我的问题是如何在 tableViewCell 中获取 UIView

或者是否有任何其他方法可以像 ios 11 Today 选项卡 View 一样获取精确的 View 单元格?这就是我想要的 ->

我已经升级了我的问题,请[这里]看看 1 : How to nest a UICollectionViewCell inside another one?

enter image description here

最佳答案

您可以像这样将 customView 添加到 uitableviewcell。

class MyTableViewCell: UITableViewCell {

    let cellView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.layer.cornerRadius  = 15
        view.backgroundColor     = UIColor.white
        view.layer.shadowColor   = UIColor.black.cgColor
        view.layer.shadowOpacity = 1
        view.layer.shadowOffset  = CGSize.zero
        view.layer.shadowRadius  = 5
        return view
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        addSubview(cellView)
    }
}

关于ios - 如何以编程方式在 UITableViewCell 中添加 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201273/

相关文章:

iphone - 如何在 mkMapview 上显示当前位置的详细信息?

ios - 在 View Controller 中重置导航栏的外观

ios - 使用手动推送导航时 Storyboard元素不可见

ios - 我想允许通过长按 1.5 秒开始直播,但我的代码要求用户在整个直播过程中按住按钮

swift - 使用 SwiftCharts 创建图表

ios - 从一个 View Controller 向另一个 View Controller 发出传递值

ios - 如何在没有模拟器的情况下模拟较小的iPhone屏幕尺寸?

ios - UICollectionView重新加载数据Swift 4

ios - Swift iOS - 在 gameScene 中设置高分

ios - 在 Interface Builder 的 View Controller 中隐藏选项卡栏