如何以编程方式在 UITableViewCell 中添加 UIView? [英] How to add UIView inside a UITableViewCell Programmatically?

查看:31
本文介绍了如何以编程方式在 UITableViewCell 中添加 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创建一个应用程序,它具有与 AppStore (iOS 11) - {Today} 视图一样的视图.

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

class MyTableViewCell: UITableViewCell {让 cellView:UIView = {让视图 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))view.layer.cornerRadius = 15view.backgroundColor = UIColor.clearview.layer.shadowColor = UIColor.black.cgColor视图.layer.shadowOpacity = 1view.layer.shadowOffset = CGSize.zeroview.layer.shadowRadius = 5返回视图}()}

在上面的类片段中,没有办法在主视图中插入我的 UIView.

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

或者有没有其他方法可以像 ios 11 Today 标签视图一样获得精确的视图单元格?这就是我想要的 ->

我已经升级了我的问题,请看[这里]

解决方案

你可以像这样将 customView 添加到 uitableviewcell 中.

class MyTableViewCell: UITableViewCell {让 cellView: UIView = {让视图 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))view.layer.cornerRadius = 15view.backgroundColor = UIColor.whiteview.layer.shadowColor = UIColor.black.cgColor视图.layer.shadowOpacity = 1view.layer.shadowOffset = CGSize.zeroview.layer.shadowRadius = 5返回视图}()覆盖 funcawakeFromNib() {super.awakeFromNib()添加子视图(单元格视图)}}

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

这篇关于如何以编程方式在 UITableViewCell 中添加 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆