简体   繁体   English

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

[英]How to add UIView inside a UITableViewCell Programmatically?

I have been creating an app which has a View as that of AppStore (iOS 11) - {Today} view.我一直在创建一个应用程序,它的视图与AppStore (iOS 11) - {Today}视图相同。

Just want to know how do I approach that view.只是想知道我如何看待这种观点。 Yet, I think the approach is to create a UIViewController with extensions of UITableViewDataSource and -Delegate, I can get the number of rows and data in my ViewController.然而,我认为该方法是创建一个带有 UITableViewDataSource 和 -Delegate 扩展的 UIViewController,我可以在我的 ViewController 中获取行数和数据。 And In the dequeReusableCell, I have created a UITableViewCell class in which I have created a UIView Programmatically but that's not working.在 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
}()
}

In the above class snippet, there is no way to insert my UIView in the main view.在上面的类片段中,无法在主视图中插入我的 UIView。

There is no method to add views(obviously) because it's under UIViewController rather than UITableViewCell.没有添加视图的方法(显然),因为它在 UIViewController 而不是 UITableViewCell 下。 So my question is how do I get UIView inside a tableViewCell所以我的问题是如何在 tableViewCell 中获取 UIView

Or Is there any other approach to get exact view cells as ios 11 Today tab view?或者有没有其他方法可以像 ios 11 Today 选项卡视图一样获得精确的视图单元格? This is what I want ->这就是我想要的->

I have upgraded my Question, please have a look [here] 1 : How to nest a UICollectionViewCell inside another one?我已经升级了我的问题,请看[这里] 1如何将 UICollectionViewCell 嵌套在另一个中?

在此处输入图片说明

You can add customView to uitableviewcell like this.您可以像这样将 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)
    }
}

have a look at this git project this will help you :)看看这个 git 项目,这会对你有所帮助:)

https://github.com/phillfarrugia/appstore-clone https://github.com/phillfarrugia/appstore-clone

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

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