我在一个视图控制器中有两个表视图。 当单击group 1按钮时,它们将被添加到TableView1,当单击group 2按钮时,它们将被添加到TableView2。 但无论我改变什么,我都会得到以下三个错误之一:
代码:
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableView2: UITableView!
var pizzas = [String]()
var drinks = [String]()
@IBAction func onAddTapped(sender:UIButton) {
pizzaCount = pizzaCount + 1
pizzaCounter.text = ("Antal pizza: ") + "\(pizzaCount)"
func add(_ pizza: String) {
pizzas.append(pizza)
tableView.insertRows(at: [IndexPath(row: pizzas.count - 1, section: 0)], with: .fade)
}
add(sender.title(for: .normal)!)
}
@IBAction func onAddTappedDrinks(sender:UIButton) {
func add(_ drink: String) {
drinks.append(drink)
tableView2.insertRows(at: [IndexPath(row: drinks.count - 1, section: 0)], with: .fade)
}
add(sender.title(for: .normal)!)
}[![enter image description here][1]][1]
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pizzas.count
}
func tableView2(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int {
return drinks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath)
}
func tableView2(_ tableView2: UITableView, cellForRowAt indexPath2: IndexPath) -> UITableViewCell {
return self.tableView2.dequeueReusableCell(withIdentifier: "item2", for: indexPath2)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? itemTableViewCell else
{ return }
let pizza = pizzas[indexPath.row]
cell.titleLabel.text = pizza
cell.detailTextLabel?.text = ("hu")
}
func tableView2(_ tableView2: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath2: IndexPath) {
guard let cell = cell as? item2TableViewCell else { return }
let drink = drinks[indexPath2.row]
cell.titleLabel.text = drink
cell.detailTextLabel?.text = ("hu")
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
pizzas.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
func tableView2(_ tableView2: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath2: IndexPath) {
guard editingStyle == .delete else { return }
drinks.remove(at: indexPath2.row)
//
tableView2.deleteRows(at: [indexPath2], with: .automatic)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
// tableView1.register(UITableViewCell.self, forCellReuseIdentifier: "CountryCell")
tableView2.dataSource = self
tableView2.delegate = self
tableView2.register(UITableViewCell.self, forCellReuseIdentifier: "item2")
}
}
这不是TableView的
UITableViewDataSource
和UITableViewDelegation
方法的工作方式。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView2(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int {
为NumberOfRowsInSection
编写2个方法不正确。 第二个不是UITableViewDataSource
方法,因此永远不会被调用。
您只需要创建一个方法,然后检查其中的TableView
和TableView2
实例,然后编写相应的代码。
例如:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return pizzas.count
} else {
return drinks.count
}
}
这适用于所有UITableViewDataSource,UITableViewDelegate方法。
您误解了如何为两个不同的表视图实现数据源方法。
拥有2个不同名称的函数作为数据源方法将无法工作:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView2(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int
第二个函数将永远不会被调用。
将为两个表视图调用第一个函数。 (TableView
参数将告诉您询问的是哪一个。) 您需要有一个switch语句来检查您被询问的表视图:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch tableView { //Switch on the parameter passed to the function
case self.tableView: //If we're being asked about our first tableView
return pizzas.count
case self.tableView2: //If we're being asked about our 2nd tableView
return drinks.count
default:
return 0 //Should never happen
}
您需要将相同的逻辑应用于所有数据源和表视图委托方法。
另外,我建议不要将名称“TableView”用于表视图实例变量。 使实例变量名称与为数据源方法传入的参数名称相同会使人困惑,并自找麻烦。 (在数据源方法中,tableview
引用作为参数传入的表视图,self.tableview
引用实例变量tableview
。相反,我建议将表视图命名为类似于Pizzastableview
和Drinkstableview
。
请注意,一个父视图控制器和两个子表视图控制器可能更容易。 每个子视图控制器都可以管理自己的数据源,这样您的数据源代码就更干净简单了。
为tableViews分配标记值并注册正确的单元格
func viewDidLoad() {
tableView.tag = 1
tableView2.tag = 2
tableView.dataSource = self
tableView.delegate = self
tableView2.dataSource = self
tableView2.delegate = self
//Register "itemTableViewCell" instead of UITableViewCell
tableView2.register(itemTableViewCell.self, forCellReuseIdentifier: "item2")
}
在委托中,检查哪个表视图请求数据
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 1 {
return pizzas.count
} else if tableView.tag == 2 {
return drinks.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath)
return cell
} else if tableView.tag == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "item2", for: indexPath)
return cell
}
}
//and so on..