如何将@IBAction func数据导入TableView?

时间:2017-11-23 17:25:14

标签: ios swift uitableview tableview

我在iOS上为我的搜索结果编写了一个tableview。

这是我的ViewController:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var resultArray = NSMutableArray()
@IBOutlet weak var scoreTableView: UITableView!
@IBOutlet weak var inputText: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
    scoreTableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return resultArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    XXXXXXX
    return cell
}

这是我的搜索和@IBAction func。

func search() -> NSMutableArray{
     XXXXXXXXX
     return array
}

@IBAction func btnSearch(_ sender: Any) {
     resultArray = search() 
}

然而,在构建之后,我的表视图是空白的。在debugDescription()中,我可以看到func search()效果很好,并打印了搜索日志。

tableview中的resultArray不是@IBAction中的empty。它是market

2 个答案:

答案 0 :(得分:0)

更改resultArray后,您必须重新加载数据。

@IBAction func btnSearch(_ sender: Any) {
  resultArray = search() 
  scoreTableView.reloadData()
}

答案 1 :(得分:0)

更新:我忘了将DataSource和Delegate分配给tableView。我从故事板手动构建了tableview。

我误解了如果我在故事板中这样做,就需要分配DataSource和Delegate。

override func viewDidLoad() {
    super.viewDidLoad()
    scoreTableView.dataSource = self
    scoreTableView.delegate = self
相关问题