Adr*_* I. 5 uitableview uiviewcontroller swift
我有一个ViewController,其中有一个TableView.在我使用Alamofire请求获取数据后,我重新加载TableView的数据,因为tableView为零,所以它崩溃了.
class TeamsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet var tableView: UITableView!
var terminArr = [Termin]()
override func viewDidLoad() {
TerminResource.getTerminsByUsername(username: "SonnyBlackzz"){
response in
self.terminArr = response
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.terminArr.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
print(self.terminArr[indexPath.row].name)
cell.textLabel?.text = self.terminArr[indexPath.row].name
return cell
}
Run Code Online (Sandbox Code Playgroud)
您将有三个选择,也许对您有帮助。
1-确保您已将插座连接到表格视图。
2-您可以将您的继承更改为如下
class TeamsVC: UITableViewController, UITableViewDelegate, UITableViewDataSource{
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
您将可以轻松访问
self.tableView.reloadData()
Run Code Online (Sandbox Code Playgroud)
3-您可能必须以编程方式初始化它
第一的
var tableView: UITableView = UITableView()
Run Code Online (Sandbox Code Playgroud)
然后在viewdidload中
tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
tableView.delegate = self
tableView.dataSource = self
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |