【问题标题】:How to control TableView in View in Swift如何在 Swift 中的 View 中控制 TableView
【发布时间】:2018-09-08 03:17:03
【问题描述】:
- 我做了一个单视图项目。
- 我在视图中添加了一个
UIView 对象
- 我在视图中添加了一个
UITableView 对象。
- 我制作了一个
TableViewController Cocoa Touch 文件来自动创建单元格。
- 为了链接
TableViewController 和UITableView,我尝试使用Identity Inspector 将表视图类更改为TableViewController,但没有成功。没有一个班级出现。
问题一:为什么不能更改表格视图类?
问题 2:如何同时控制视图和表格视图?
【问题讨论】:
标签:
swift
uitableview
tableview
【解决方案1】:
1) 您无法更改 tableView 的类,因为该对象是 UITableView 类型的,而您正尝试将其设置为 UITableViewController
类型的类
2)要同时控制View和TableView,可以设置outlet。
class YourViewController: UIViewController, UITableViewDelegate, UITableViewDatasource {
// Connect the outlets to storyboard's YourViewController UI
@IBOutlet var tableView: UITableView!
@IBOutlet var yourView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Set delegate and datasource and implement the delegate methods
self.tableView.dataSource = self
self.tableView.delegate = self
// Handle the view properties for yourView here
self.yourView.backgroundColor = UIColor.red
}
}
希望对你有帮助