【发布时间】:2019-05-13 14:35:24
【问题描述】:
我有tableview 有两个部分,每个部分有一排。
我想在单击按钮时填写tableview 数据。
在viewDidLoad 我隐藏tableView。
单击按钮时,我将datasource 和delegate 分配给tableview 并取消隐藏tableview,但它的datasource 方法没有被调用。
下面是代码:
@IBAction func btnShowAction(_ sender: Any) {
tableView.delegate = self
tableView.dataSource = self
tableView.isHidden = false
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return ((tableView.frame.size.height / 2) - 20)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 0) {
return "Section 1"
}
else {
return "Section 2"
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
tableViewCell.lblText.text = "abc"
return tableViewCell
}
【问题讨论】:
-
为什么要在按钮操作中设置数据源和委托?
-
如果需要,我想根据用户请求在tableview中填写数据
-
彻坦,你需要重新加载你的tableview,如果你想将你更新的数据显示到TableView
-
这不合理。
-
@abdul 它不会在我设置数据源时自动重新加载数据?
标签: ios swift uitableview datasource