How to add UIView inside a UITableViewCell Program

2019-06-10 15:20发布

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?

enter image description here

2条回答
我命由我不由天
2楼-- · 2019-06-10 16:07

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.clear
        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)
    }
}
查看更多
Luminary・发光体
3楼-- · 2019-06-10 16:12

have a look at this git project this will help you :)

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

查看更多
登录 后发表回答