斯威夫特3.0 iOS 10.x
转到TableViewController,传递数据;但是它没有正确显示;我看到第一个数据行两次了?[只传递了两行数据]。
TableView看起来像这样。。锅炉板代码或多或少。
import UIKit
class QRtableViewController: UITableViewController, UIGestureRecognizerDelegate {
var beaconArray:[String] = []
@IBOutlet var beaconTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action:#selector(handleTap))
// 4
recognizer.delegate = self
view.addGestureRecognizer(recognizer)
print("beaconArray II \(beaconArray)")
beaconTable.delegate = self
beaconTable.dataSource = self
beaconTable.register(UITableViewCell.self, forCellReuseIdentifier: "theCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(beaconArray[indexPath.row])")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return beaconArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theCell", for: indexPath as IndexPath)
cell.textLabel!.text = "\(beaconArray[indexPath.row])"
cell.textLabel?.font = UIFont(name: "AvenirNextCondensed", size: 6)
cell.textLabel?.textAlignment = .center
return cell
}
}
从电视上看。。。
func handleTap(recognizer: UITapGestureRecognizer) {
performSegue(withIdentifier: "beaconList", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let viewControllerB = segue.destination as? QRtableViewController {
print("\(beaconArray)")
viewControllerB.beaconArray = beaconArray
}
}
在我穿过之前和之后都检查了beaconArray。看起来还好吧?
beaconArray I["Venus CLBeacon (uuid:2F234454-CF6D-4A0F-ADF2-F4911BA9FFC4, major:4, minor:1, proximity:3 +/- 7.25m, rssi:-80)", "Mars CLBeacon (uuid:2F234454-CF6D-4A0F-ADF2-F4911BA9FFA2, major:1, minor:7, proximity:3 +/- 4.64m, rssi:-78)"]
beaconArray II ["Venus CLBeacon (uuid:2F234454-CF6D-4A0F-ADF2-F4911BA9FFC4, major:4, minor:1, proximity:3 +/- 7.25m, rssi:-80)", "Mars CLBeacon (uuid:2F234454-CF6D-4A0F-ADF2-F4911BA9FFA2, major:1, minor:7, proximity:3 +/- 4.64m, rssi:-78)"]
但我明白了?
我错过了什么?
最佳答案
你正在混合两种委托方法。应该是的
// override func numberOfSections(in tableView: UITableView) -> Int {
// return 1
// }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return beaconArray.count
}
numberOfSections
的默认值为1,因此可以忽略它。