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

时间:2018-09-06 09:47:12

标签: ios swift xcode uitableview uiviewcontroller

我一直在创建一个具有与 AppStore(iOS 11)-{Today} 视图相同的视图的应用程序。

只想知道我如何处理这种观点。但是,我认为该方法是创建一个具有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。

(显然)没有添加视图的方法,因为它位于UIViewController而不是UITableViewCell下。 所以我的问题是如何在tableViewCell

中获取UIView

或者还有其他方法来获取ios 11 Today选项卡视图中的确切视图单元格吗?这就是我想要的->

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

enter image description here

2 个答案:

答案 0 :(得分:2)

  

看看这个git项目,它将对您有帮助:)

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

答案 1 :(得分:2)

您可以像这样将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.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)
    }
}