gpt4 book ai didi

swift - TableView 有 3 个部分且单元格崩溃

转载 作者:行者123 更新时间:2023-11-30 13:49:29 25 4
gpt4 key购买 nike

我有一个简单的 UITableViewController,包含 3 个部分,每个部分有 3 行。我已经为每个单元格分配了重用标识符。当我运行代码时,我在返回 cell 时收到 EXE_BAD_INSTRUCTION 错误。

这是我的代码:

import UIKit

class settingsViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {


return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}


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

var cell : UITableViewCell!

if indexPath.section == 1
{
if indexPath.row == 1
{

cell = tableView.dequeueReusableCellWithIdentifier("draftCell", forIndexPath: indexPath) as UITableViewCell

}

else if indexPath.row == 0
{
cell = tableView.dequeueReusableCellWithIdentifier("profileCell", forIndexPath: indexPath) as UITableViewCell




}

else if indexPath.row == 2
{

cell = tableView.dequeueReusableCellWithIdentifier("logoutCell", forIndexPath: indexPath) as UITableViewCell



}






}
else if indexPath.section == 2{

if indexPath.row == 0
{



cell = tableView.dequeueReusableCellWithIdentifier("facebookCell", forIndexPath: indexPath) as UITableViewCell

}


else if indexPath.row == 1
{


cell = tableView.dequeueReusableCellWithIdentifier("twitterCell", forIndexPath: indexPath) as UITableViewCell


}


else if indexPath.row == 2
{
cell = tableView.dequeueReusableCellWithIdentifier("rateusCell", forIndexPath: indexPath) as UITableViewCell

}



}



else if indexPath.section == 3
{

if indexPath.row == 0
{


cell = tableView.dequeueReusableCellWithIdentifier("creditsCell", forIndexPath: indexPath) as UITableViewCell

}



else if indexPath.row == 1
{

cell = tableView.dequeueReusableCellWithIdentifier("contactusCell", forIndexPath: indexPath) as UITableViewCell
}


else if indexPath.row == 2
{


cell = tableView.dequeueReusableCellWithIdentifier("termsandconditionsCell", forIndexPath: indexPath) as UITableViewCell
}


}


return cell


}

最佳答案

对于第 0 节,您尚未编写 cellForRowAtIndexPath。您给定的节数 = 3,因此其索引从 0 开始,

更正你的indexpath.section比较,即对indexpath.section == 0、indexpath.section == 1和indexpath.section == 2进行比较。

此外,您还没有为 tableview 单元格注册类

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

关于swift - TableView 有 3 个部分且单元格崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34471744/

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