在viewController中使用tableViewCell添加视图的方法如下:
以下是一个示例代码:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// 设置tableView的代理和数据源
tableView.delegate = self
tableView.dataSource = self
// 注册tableViewCell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// 添加tableView到viewController的视图中
tableView.frame = view.bounds
view.addSubview(tableView)
}
// MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// 添加需要的视图到tableViewCell的contentView中
let subview = UIView()
subview.backgroundColor = .red
subview.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
cell.contentView.addSubview(subview)
return cell
}
}
这样,在viewController中使用tableViewCell添加视图的过程就完成了。在tableView(_:cellForRowAt:)方法中,可以根据需要添加任意类型的视图,并进行相应的布局设置。
没有搜到相关的文章