gpt4 book ai didi

uitableview - numberOfRowsInSection 导致 swift 3 崩溃

转载 作者:行者123 更新时间:2023-12-04 03:02:40 25 4
gpt4 key购买 nike

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

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

}

我想用数组中的 6 个项目填充表格。我打印了数组计数来确认。这是全 View Controller 。
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 + Command + 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 应该是动态的,但我不相信它会导致崩溃。

关于uitableview - numberOfRowsInSection 导致 swift 3 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com