swift - 在一个 View Controller 中将具有多个 TableView 的自定义单元格出队

标签 swift uitableview

我试图在同一个 View Controller 中使用自定义单元格使多个 TableView 出队我不断收到错误消息(“UITableViewCell 类型的值?”没有成员“serviceChoiceAmount”、“serviceChoicePrice”、“serviceChoiceTitle”、“serviceChoiceImage”)下面我有 View Controller 和一个自定义单元格的代码(其他人会出现相同的错误)。 (我没有输入其他单元格的所有代码,如果我有解决此错误的方法,我会这样做),我应该为自定义单元格类中的单元格创建一个初始化程序并在 View Controller 中使用它出列吗?

View Controller

import UIKit

class CartViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {





let defaults = UserDefaults.standard
var TOTAL = [Double]()
@IBOutlet weak var cartTableView: UITableView!
@IBOutlet weak var totalTableView: UITableView!

@IBOutlet weak var optionsTableView: UITableView!
@IBOutlet weak var choosePayment: UITableViewCell!
@IBOutlet var address: [UITableViewCell]!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    let Cartcell = defaults.array(forKey: "Cart")
    print(Cartcell as Any)
    print(TOTAL)
    print("ViewDidLoad")
}

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

@IBAction func clearCart(_ sender: Any) {
    //add function to clear total cell
    CartSingleton1.sharedInstance.orderDict.removeAll()
    cartTableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
    var section: Int?
    if tableView == self.cartTableView{
        section = 1
    }
    if tableView == self.totalTableView{
        section = 1
    }
    if tableView == self.optionsTableView{
        section = 3
    }
    return section!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    var title: String?
    if tableView == self.cartTableView{
        title = ("Order Details")
    }
    if tableView == self.totalTableView{
        title = ("Total")
    }
    if tableView == self.optionsTableView{
        title = ("Payment Option")
        //dropoff address
        //pickup address
    }
    return title!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count: Int?
    if tableView == self.cartTableView{
    count = CartSingleton1.sharedInstance.orderDict.count
    }
    if tableView == self.totalTableView{
        count = 1
    }
    if tableView == self.optionsTableView{
        count = 3
    }
    return count!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: UITableViewCell?
    if tableView == self.cartTableView{
    let cart = CartSingleton1.sharedInstance.orderDict[indexPath.row] as NSDictionary
    cell = tableView.dequeueReusableCell(withIdentifier: "ServiceCart") as! CartTableViewCell
    cell.serviceChoiceImage.image = cart.value(forKey: "image") as? UIImage
    cell.serviceChoicePrice.text = cart.value(forKey: "pricing") as? String
    cell.serviceChoiceTitle.text = cart.value(forKey: "titled") as? String
    let quantityInt = cart.value(forKey: "quantity") as? Int
    cell.serviceChoiceAmount.text = quantityInt?.description
    }
    if tableView == self.totalTableView{

    }
    if tableView == self.optionsTableView{

    }
    return cell!
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if tableView == self.cartTableView{
    if editingStyle == .delete{
        print("deleted")

        CartSingleton1.sharedInstance.orderDict.remove(at: indexPath.row)
        self.cartTableView.deleteRows(at: [indexPath], with: .automatic)
        print("cart dictionary delete", CartSingleton1.sharedInstance.orderDict)
    }
    }
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

自定义单元格:CartTableViewCell

import UIKit

class CartTableViewCell: UITableViewCell {

@IBOutlet weak var serviceChoiceImage: UIImageView!
@IBOutlet weak var serviceChoiceTitle: UILabel!
@IBOutlet weak var serviceChoiceAmount:UILabel!
@IBOutlet weak var serviceChoicePrice: UILabel!



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

最佳答案

您的问题是您已使用此行将 cell 声明为 UITableViewCell? 类型:

var cell: UITableViewCell?

因此,当您将单元格出队并将其分配给此变量时,它会采用该类型。因此,即使它实际上是一个 CartTableViewCell,Swift 也只会让您调用可用于 UITableViewCell 的方法和访问属性。

摆脱该声明并将您的 dequeue 行更改为:

let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceCart") as! CartTableViewCell

cell 将是 CartTableViewCell 类型,您将能够访问其属性。由于 cell 不再是可选的,因此更改您的:

return cell!

到:

return cell

您需要将此 return 移动到您的 if block 中,或者将 cell 分配给另一个变量以在结束时返回功能。


或者,将 cell 更改为 tableCell 并仅使用它来返回单元格值:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var tableCell: UITableViewCell?
    if tableView == self.cartTableView {
        let cart = CartSingleton1.sharedInstance.orderDict[indexPath.row] as NSDictionary
        let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceCart") as! CartTableViewCell
        cell.serviceChoiceImage.image = cart.value(forKey: "image") as? UIImage
        cell.serviceChoicePrice.text = cart.value(forKey: "pricing") as? String
        cell.serviceChoiceTitle.text = cart.value(forKey: "titled") as? String
        let quantityInt = cart.value(forKey: "quantity") as? Int
        cell.serviceChoiceAmount.text = quantityInt?.description
        tableCell = cell
    }
    if tableView == self.totalTableView {

    }
    if tableView == self.optionsTableView {

    }
    // safely unwrap tableCell and return UITableViewCell() if nothing was assigned
    return tableCell ?? UITableViewCell()
}

关于swift - 在一个 View Controller 中将具有多个 TableView 的自定义单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017616/

相关文章:

ios - PageViewController 使我的 PickerView 不出现

ios - 函数运行时显示 UIView/UIImage/UITextView

ios - 如何在 iPhone 中更改 UItableview 的高度和宽度

ios - 如何在uiTableViewCell 内的uiTextField 下画一条线?

swift - 泛型的扩展在 swift 中被限制为另一个泛型

swift - Date() 总是初始化 UTC 日期吗?

swift - 更改背景颜色但保持 UISwitch "On"状态的色调

iphone - uitableview 中的复选框出现错误

Swift根据数据改变tableviewcell的边框颜色

ios - UITableViewCellAccessory 多节