问题
I have written this code:
//Basic methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.DevicesTableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
self.DevicesTableView.dataSource = self
}
var array = ["one","two"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell! = self.DevicesTableView
.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cell.textLabel!.text = self.array[indexPath.row]
return cell
}
So the code works pretty well and shows me the tableview filled with:
Row1 "One" Row2 "Two"
But how can I fill the TableView not at the beginning of my program but anytime else? I tried to call the methods below in an other function that viewDidLoad but it doest't work... why?
self.DevicesTableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: "cell")
self.DevicesTableView.dataSource = self
EDIT:
I would like to display an array which isn't initialized at the beginning of my program. It contains some BLE devices after the user searched for them.
回答1:
Its very simple
- Update your data in your array
- Then reload your tableview by using your_tableview.reloadData()
Ex:
var array = ["one","two","Three","Four"] // your array will take no.of.row in section
DevicesTableView.reloadData()
回答2:
Table views have a reloadData
method which does what you want.
来源:https://stackoverflow.com/questions/32799098/how-to-fill-a-table-view-in-swift