Uitableview swift 3中导致碰撞的部分中的行数
我正在尝试构建uitableviewcontroller,但在处理swift 3时遇到困难。无论何时调用numberOfRowsInSection,它都会被调用4次,然后应用程序崩溃。有人知道如何实施这一is swift 3吗Uitableview swift 3中导致碰撞的部分中的行数,uitableview,swift3,Uitableview,Swift3,我正在尝试构建uitableviewcontroller,但在处理swift 3时遇到困难。无论何时调用numberOfRowsInSection,它都会被调用4次,然后应用程序崩溃。有人知道如何实施这一is swift 3吗 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 6; } 数组中有6项我想用它们填充表。我打印
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;
}
}
类PCRInpatientsViewController:UITableViewController
{
var listInspital=[PCRPatient]();
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画);
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.separatorStyle=UITableViewCellSeparatorStyle.none;
self.title=“住院患者”;
让视图=self.view.frame;
让背景=UIView(帧:视图);
background.backgroundColor=Constants.medisasDarkGrey;
self.tableView.backgroundView=背景;
self.definesPresentationContext=true;
获取患者();
createUI();
}
func getPatients(){
var数组=[PCRPatient]();
设i=Patients.sharedInstance.Patients
对于0..int中的int{
返回6;
}
func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
返回150;
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->PCRCustomCell{
let patient=listInspitality[indexPath.row];
打印(“sjddsodkso\(患者姓名)”;
let cell=PCRCustomCell(重复使用:“住院患者”,患者:患者);
cell.contentView.backgroundColor=Constants.medisasgray;
返回单元;
}
}
您需要在Swift 3中加入一些语法更改。Xcode 8 beta通常会建议对您的代码进行正确的修复,但并不总是这样。在发现您可以通过点击Shift+Command+0
并搜索almo的前几个字符来检查Xcode 8 beta中更新的文档之前,我一直在努力解决这个问题在本例中,搜索UITableViewDataSource
,您将看到我将要注意的所有内容
1) cellForRowAtIndexPath
现在是cellForRowAt
nsindepath
现在是indepath
。实现如下:
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
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return float
}
2) heightForRowAtIndexPath
现在类似于heightForRowAt
。我个人并没有实现这一点,但从文档中可以看出,它是这样实现的:
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
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return float
}
3) 正如其他人所指出的,numberOfRowsInSection应该是动态的,但我不认为它会导致崩溃。我检查了一下,数组中有6个项目。我添加了全屏视图控制器,使其保持原来的状态,但我将其更改为6,因为它正在崩溃,我想看看如果我写6,它是否会崩溃。尽管如此,它仍然崩溃。这在旧的swift中可以很好地工作,这就是为什么我认为swift 3的某些更改是原因。我想我会听从更了解情况的人的建议。我期待看到问题所在。总是在访问数组的1个对象
if(listInspitality.count>indexPath.row)之前验证数组…//做点什么
不要在numberOfRowsInSection
中硬编码返回值,始终使用listInspitality.count
。实际上,您可以将整个方法getPatients()
写在一行:listInspitality=Patients.sharedInstance.Patients.filter{$0.status==preadmin}
。去掉分号,Swift中不需要分号。