[英]How to add UIView inside a UITableViewCell Programmatically?
我一直在创建一个应用程序,它的视图与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 选项卡视图一样获得精确的视图单元格? 这就是我想要的->
我已经升级了我的问题,请看[这里] 1 : 如何将 UICollectionViewCell 嵌套在另一个中?
您可以像这样将 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)
}
}
看看这个 git 项目,这会对你有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.