uitableview – numberOfRowsInSection导致swift 3崩溃

我正在尝试构建一个uitableviewcontroller并且很难处理
swift 3.每当调用numberOfRowsInSection时,它会被调用4次然后应用程序崩溃.有谁知道如何实现这是迅捷3?

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 6;

}

数组中有6个项目要填充表格.我打印了数组计数以确认.这是全视图控制器.

class PCRInpatientsViewController: UITableViewController
{
var listInpatient = [PCRPatient]();

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated);


    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none;

    self.title = "Inpatients";

    let view = self.view.frame;
    let background = UIView(frame: view);
    background.backgroundColor = Constants.medisasDarkGrey;
    self.tableView.backgroundView = background;

    self.definesPresentationContext = true;

    getPatients();
    createUI();

}

func getPatients() {

    var array = [PCRPatient]();
            let i = Patients.sharedInstance.patients

            for int in 0..<i.count {
                let d = i[int];

                if d.status == PCRPatientStatus.PreAdmit {
                    print(d.name)
                    array.append(d);
                }


            }
    print(array.count);
    self.listInpatient = array;


}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated);

}

func createUI (){


    self.navigationController?.navigationBar.barTintColor = Constants.medisasRed;
    self.navigationController?.navigationBar.isTranslucent = false;
    self.navigationController?.navigationBar.tintColor = UIColor.white();
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white()];


}



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 6;

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 150;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> PCRCustomCell {

    let patient = listInpatient[indexPath.row];
    print("sjddsodkso \(patient.name)");

    let cell = PCRCustomCell(reuse: "Inpatient", patient: patient);
    cell.contentView.backgroundColor = Constants.medisasGrey;


    return cell;
}

}

最佳答案 Swift 3中的一些语法更改需要合并. Xcode 8 beta通常会为您的代码提供正确的修复,但并非总是如此.我发现你可以通过点击Shift命令0并搜索几乎任何东西的前几个字符来查看Xcode 8 beta中的更新文档.在这种情况下,搜索UITableViewDataSource,你会看到我要注意的一切.

1)cellForRowAtIndexPath现在是cellForRowAt. NSIndexPath现在是IndexPath.实现如下:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // if you use prototype cells
    let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeCellIdentifier") as! CustomUITableViewCell

    // configure your cell

    return cell
}

2)heightForRowAtIndexPath现在类似于heightForRowAt.我没有亲自实现这个,但是从文档中实现它是这样的:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return float
}

3)正如其他人所说,numberOfRowsInSection应该是动态的,但我不相信它会导致崩溃.

点赞