寻找一个tableview控制器,通过

时间:2017-08-03 18:41:53

标签: ios swift uitableview tableview segue

Swift 3.0 iOS 10.x

Segue到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
}
}

和TVC看起来像这样......

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)"]

但我知道了吗?

enter image description here

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您正在混合使用两种委托方法。它应该是

// override func numberOfSections(in tableView: UITableView) -> Int {
//    return 1
// }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return beaconArray.count
}

numberOfSections的默认值为1,因此可以省略。