Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何增加或减少2个标签取决于tavleview单元格中的步进操作_Swift_Xcode - Fatal编程技术网

Swift 如何增加或减少2个标签取决于tavleview单元格中的步进操作

Swift 如何增加或减少2个标签取决于tavleview单元格中的步进操作,swift,xcode,Swift,Xcode,如何增加tableview单元格内的2个标签或减少取决于步进器操作。 下图显示了我编写的所有代码 import UIKit class AnimalsTableViewCell: UITableViewCell { @IBOutlet weak var animalImage: UIImageView! @IBOutlet weak var animalTitle: UILabel! @IBOutlet weak var animalPri

如何增加tableview单元格内的2个标签或减少取决于步进器操作。 下图显示了我编写的所有代码

import UIKit

class AnimalsTableViewCell: UITableViewCell
{
    
    @IBOutlet weak var animalImage: UIImageView!
    
    @IBOutlet weak var animalTitle: UILabel!
    
    @IBOutlet weak var animalPrice: UILabel!
    
    @IBOutlet weak var animalCount: UILabel!
    
    @IBOutlet weak var stepperCount: UIStepper!
    
    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
    }
    
    @IBAction func stepperPressed(_ sender: UIStepper)
    {
        // how to increment or decrement depends if the user pressed + or -
        
        // increment the price of the label (animalPrice) by for example 10 or decrement it
        
        // increment the counter of the label (animalCount) by 1 or decrement it by 1
    }
    

}

import UIKit

class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource
{
    
    @IBOutlet weak var tableView1: UITableView!
    
    let animals = [#imageLiteral(resourceName: "tiger") , #imageLiteral(resourceName: "cat") , #imageLiteral(resourceName: "lion") , #imageLiteral(resourceName: "dog")]      //randoms images
    
    let animalTitle = ["tiger" , "cat" , "lion" , "dog"]    //random animals names
    
    let animalWeight = ["20kg" , "10kg" , "20kg" , "25kg"]   //random weight
    
    let animalPrice = ["20$" , "30$" , "40$" , "60$"]   //random price
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        tableView1.dataSource = self
        tableView1.delegate = self
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 115
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        animals.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AnimalsTableViewCell
        
        return cell
    }
    
}

试着写一些像这样的东西

yourLabel.text = "\(Your updated score)"
试试这个代码

 @IBAction func stepperPressed(_ sender: UIStepper){
        var price = Int(animalPrice.text ?? "") ?? 0
        
        sender.value == 1.0 ? (price = price + 10) : (price = price - 10)

        self.animalPrice.text = "\(price)"
        
        sender.value = 0
    }
 
//步进动作

当您增加/减少幅度更大时,价格/数量将相应增加/减少

//为UIStepper设置这两个属性

步进计数最小值=0 步进计数最大值=10

@IBAction func stepperPressed(_ sender: UIStepper) {

    let selectedValue = Int(sender.value) //Getting stepper current value  
    animalCount.text = "\(selectedValue)"
    
    if let intAnimalPrice = Int("20$".replacingOccurrences(of: "$", with: "")) {

        animalPrice.text = "\(Int(selectedValue) * intAnimalPrice)"
    }
}

尝试代码并对animalCountApart执行相同的操作。从问题开始,您必须更新数据模型,否则在用户滚动时会遇到意外行为。并且永远不要使用多个数组作为数据模型。这是非常糟糕的做法。