微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 为什么tableview没有?

我有一个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
}
您将有三个选项可能会帮助您.

1 – 确保已将插座连接到工作台视图.

2 – 您可以将您的继承更改为如下所示

class TeamsVC: UITableViewController,UITableViewDataSource{
.
.
.
}

你将很容易访问

self.tableView.reloadData()

3-您可能必须以编程方式初始化它

第一

var tableView: UITableView  =   UITableView()

然后在viewdidload中

tableView = UITableView(frame: UIScreen.mainScreen().bounds,style: UITableViewStyle.Plain)
tableView.delegate      =   self
tableView.dataSource    =   self

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐