使用变量更新数组中的初始化数据,然后将数组传递给下一个视图控制器

Update initialized Data in array with variable then pass array to next view controller

我在将所选单元格的数据从 (service2viewcontroller) 移动到 (confirmorderviewcontroller) 时遇到问题。我正在尝试移动单元格数据(stepper.value 大于 0 的单元格(变量数量 > 0.0(在 Service2ViewController 中))),有人告诉我将数组传递给下一个视图控制器,以便为步进器这样做值大于 0 我还需要为数量变量大于 0 的行发送 indexpath.row 正确吗?我不知道该怎么做,如果有人可以提供帮助,我将不胜感激。当我使用步进器时标签也没有更新,它保持在 0,我可以将数量变量放在数组中吗?视图控制器中的总价标签继续运行,数据毫无问题地发送到 (confirmorderviewcontroller)。

第一个TableView(数据输入转发)

class Service2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var service2Total: UILabel!

@IBOutlet weak var service2TableView: UITableView!

// service data
var Wash: [Service2] = []

//stepper and price calculation
var quantity = Double()

var totalPrice : Double = 0.0
var priceList = [Int : Double]()
var totalProductPrice = [Int : Double]()
var label : Int!
override func viewDidLoad() {
    super.viewDidLoad()

    Wash = Options2()

    if Int(quantity) > 0{
        service2TableView.reloadData()
    }

        priceList[0] = 3.51//price list
        priceList[1] = 5.51


   service2Total.text = "[=10=].00"

}

// create data array
func Options2() -> [Service2]{

    var washOptions: [Service2] = []

    let option1 = Service2(titled: "Test", pricing: ".51", image: #imageLiteral(resourceName: "Wash&Fold"), description:"Testing the description box", quantity: Int(quantity))
    let option2 = Service2(titled: "Test", pricing: ".51", image: #imageLiteral(resourceName: "Wash&Fold"), description: "Testing the description box", quantity: Int(quantity))

    washOptions.append(option1)
    washOptions.append(option2)

    return washOptions
}
func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Wash.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Wash1 = Wash[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "Service2Cell", for: indexPath) as! Service2TableViewCell

    cell.setService(Wash: Wash1)



    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 133
}




@IBAction func stepperAcn(_ sender: UIStepper) {

    //change label value with stepper
    let cellPosition = sender.convert(CGPoint.zero, to: service2TableView)
    let indPath : IndexPath = service2TableView.indexPathForRow(at: cellPosition)!

    quantity = sender.value

    let getCurrentProductPrice : Double = priceList[indPath.row]! * sender.value

    totalPrice = gettingPriceLabel(indPath: indPath, getCurrentProductPrice: getCurrentProductPrice)


    if totalPrice == 0{
        service2Total.text = ("[=10=].00")
    }
    else{
    service2Total.text = ("$")+String(totalPrice)
    }


    print("total price",totalPrice)
    print("quantity double",quantity)
service2TableView.reloadData()


}
func gettingPriceLabel(indPath: IndexPath, getCurrentProductPrice : Double) -> Double
{

    totalProductPrice[indPath.row] = getCurrentProductPrice

    var totalCost : Double = 0.0

    let valuesArr = Array(totalProductPrice.values)

    for i in 0..<valuesArr.count
    {
        totalCost = totalCost + valuesArr[i]
    }
    return totalCost
}
// add function to collect (didSelectRowAt) and send selected data to cart and prepare for segue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



}
// change sender to
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController: ConfirmorderViewController = segue.destination as! ConfirmorderViewController


    if totalPrice > 0.00{
    DestViewController.amount = totalPrice
    }
}
}

服务初始化器

 class Service2
{
var service2Title: String
var service2Image: UIImage
var Service2Pricing: String
var service2Description: String
var service2Quantity: Int


init(titled: String, pricing: String, image: UIImage, description: String, quantity: Int){
    self.service2Title = titled
    self.Service2Pricing = pricing
    self.service2Image = image
    self.service2Description = description
    self.service2Quantity = quantity
}
}

服务 2 TableViewCell

class Service2TableViewCell: UITableViewCell {
@IBOutlet weak var service2Title: UILabel!

@IBOutlet weak var service2Stepper: UIStepper!
@IBOutlet weak var service2StepperLbl: UILabel!
@IBOutlet weak var service2Pricing: UILabel!
@IBOutlet weak var service2Image: UIImageView!
@IBOutlet weak var service2Description: UILabel!

func setService(Wash: Service2){
    service2Image.image = Wash.service2Image
    service2Pricing.text = Wash.Service2Pricing.description
    service2Title.text = Wash.service2Title
    service2Description.text = Wash.service2Description
    service2StepperLbl.text = Wash.service2Quantity.description
}
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
}

}

第二个 TableView(接收数据)

 class ConfirmorderViewController: UIViewController{



@IBOutlet weak var Total: UILabel!
@IBOutlet weak var confirmOrderTableView: UITableView!



var titled = [String]()
var amount: String = ""
//var quantity = String()
var image1 = [UIImage]()
var Price = [Double]()



override func viewDidLoad() {
    super.viewDidLoad()

    Total.text = amount


    confirmOrderTableView.reloadData()
}

}
extension ConfirmorderViewController: UITableViewDataSource, UITableViewDelegate{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return titled.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

    cell.coTitle?.text = titled[indexPath.row]
    cell.coImg?.image = image1[indexPath.row]
    //cell.coQuantity.text = quantity
    cell.coPrice?.text = Price.description

    return cell
}


}

我在这里试过了。我得到了 row numbers 的列表,订单数超过 0。我把它存储在 whichRowToBeAdd 中。如果用户 decreasedZero,相应的行将 removed 来自此 array

Singleton Class的帮助下,我们可以存储我们需要在NextViewController

中显示的任何内容
var whichRowToBeAdd = [Int]() // GLOBAL

@IBAction func stepperAcn(_ sender: UIStepper) {

//change label value with stepper
let cellPosition = sender.convert(CGPoint.zero, to: service2TableView)
let indPath : IndexPath = service2TableView.indexPathForRow(at: cellPosition)!


    if Int(sender.value) == 0
    {
        if whichRowToBeAdd.contains(indPath.row)
        {
            whichRowToBeAdd.remove(at: whichRowToBeAdd.index(of: indPath.row)!)
        }
        else
        {

        }
    }
    else
    {
        if whichRowToBeAdd.contains(indPath.row)
        {

        }
        else
        {
            whichRowToBeAdd.append(indPath.row)
        }
    }

//.... 
//..... Your Code in your answer


}

// After stepper Action, final click of Button, which moves to Next ViewController
@IBAction func goToConfirmOrder(_ sender: UIBarButtonItem) { 

    print("\n\n Val_    ", whichRowToBeAdd)

    singleTon.sharedInstance.orderDict.removeAll()

    for i in 0..<whichRowToBeAdd.count
    {
        let indPath = IndexPath(row: whichRowToBeAdd[i], section: 0)

        let newCell = tblVw.cellForRow(at: indPath) as! Service2TableViewCell
        print("qweqwe      ",newCell.testLbl.text)

        let name : String = newCell.service2Title.text!
        let image : UIImage = newCell.service2Image.image
        let quantity : Int = Int(newCell.service2StepperLbl.text!)!

        getOrderOneByOne(productName: name, productImage: image, productQuantity: quantity)

        if i == (whichRowToBeAdd.count - 1)
        {
            self.performSegue(withIdentifier: "confirmOrderVC", sender: nil)
        }
    }

}

func getOrderOneByOne(productName: String, productImage : UIImage, productQuantity: Int)
{


    let createDict = ["productName" : productName, "productImage" : productImage, "productQuantity" : productQuantity] as [String : Any]

    singleTon.sharedInstance.orderDict.append(createDict)

}

单例Class

class singleTon: NSObject {

    static let sharedInstance = singleTon() // Singleton Instance

    var orderDict = [[String : Any]]() // Dictionary Declaration
}

下一个ViewController

class ConfirmOrderViewController: UIViewController {
   override func viewDidLoad() {
     super.viewDidLoad()

     print("\n\norderDict.coun    ", singleTon.sharedInstance.orderDict)
   }
}

这样就可以在这个ConfirmOrderViewController的TableView中显示数据了。