简体   繁体   中英

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.

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. And In the dequeReusableCell, I have created a UITableViewCell class in which I have created a UIView Programmatically but that's not working.

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.

There is no method to add views(obviously) because it's under UIViewController rather than UITableViewCell. So my question is how do I get UIView inside a tableViewCell

Or Is there any other approach to get exact view cells as ios 11 Today tab view? This is what I want ->

I have upgraded my Question, please have a look [here] 1 : How to nest a UICollectionViewCell inside another one?

在此处输入图片说明

You can add customView to uitableviewcell like this.

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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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