当我点击放置在合适视图单元格中的步进器时,我无法更新标签
i'm not able to update the label when i tapped on stepper which is placed in uitable view cell
here is the image i had designed
这是 UITableviewCell
的代码,在此我放置了步进动作方法来触发,但是当我点击 stepper
时无法更新价格标签,有人能帮我吗?
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper!
@IBOutlet var imageview: UIImageView!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var quantityTextField: UITextField!
var pricearr = [String]()
var price : String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBAction func changeCart(_ sender: Any) {
let value = Int(stepper.value)
quantityTextField.text = String(value)
}
}
这是 UITableViewCell
的代码,在此我放置了步进器动作方法来触发,但是当我点击步进器时无法更新价格标签,有人能帮我吗?
@IBOutlet var tableDetails: UITableView!
var productsArray = [String]()
var nameArray = [String]()
var priceArray = [String]()
let urlstring = "http://www.json-generator.com/api/json/get/ceghMiWudK?indent=2"
var price = [String]()
var sum = 0
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
tableDetails.delegate = self
tableDetails.dataSource = self
tableDetails.alwaysBounceVertical = false
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlstring)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "Detail"))
if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
for item in detailsArray {
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "productName"){
self.nameArray.append(name as! String)
}
if let price = detailDict.value(forKey: "productPrice"){
self.priceArray.append(price as! String)
}
if let image = detailDict.value(forKey: "img"){
self.productsArray.append(image as! String)
}
}
}
}
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
for item in self.priceArray{
let endIndex = item.index(item.endIndex, offsetBy: -5)
let truncated = item.substring(to: endIndex)
self.price.append(truncated)
}
print(self.price)
})
}
}).resume()
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if (section == 0){
return productsArray.count
}else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! productTableViewCell
let arr = self.productsArray[indexPath.row]
let urls = NSURL(string: arr)
let data = NSData (contentsOf: urls! as URL)
cell.imageview.image = UIImage(data: data! as Data)
cell.nameLabel.text = nameArray[indexPath.row]
let x = priceArray[indexPath.row]
let array = String(x)
cell.priceLabel.text = array
cell.quantityTextField.text = "1"
cell.pricearr = [price[indexPath.row]]
return cell
}else if indexPath.section == 1{
let cell = tableView.dequeueReusableCell(withIdentifier: "couponcell", for: indexPath) as! CouponTableViewCell
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "checkout", for: indexPath) as! checkoutTableViewCell
return cell
}
}
我认为问题出在下面这行
priceLabel.text = x * String(value)
你想在这里完成什么?这不应该编译
如下图所示添加步进器的出口和动作:
将您的代码替换为以下代码:
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper!
@IBOutlet var imageview: UIImageView!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var quantityTextField: UITextField!
var pricearr = [String]()
var price : String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBAction func changeCart(_ sender: Any) {
let value = Int(stepper.value)
quantityTextField.text = String(value)
let x = Int(pricearr[0])
priceLabel.text = String(x! * value)
}
}
Your code has one bug is that when you scroll tableView that time cell will be reset to its default value because of cell reuse.
编辑:
实现目标的更好方法(据我所知)可以定义如下:
- 删除步进动作
从 UITableview 单元格中删除操作 class
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper!
@IBOutlet var imageview: UIImageView!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var quantityTextField: UITextField!
var price : String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
3.Replace 你的核心逻辑和下面的代码
@IBOutlet var tableDetails: UITableView!
var CartArray : [[String: String]] = []
var itemsArray : [[String: AnyObject]] = []
let urlstring = "http://www.json-generator.com/api/json/get/ceghMiWudK?indent=2"
var sum = 0
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
tableDetails.delegate = self
tableDetails.dataSource = self
tableDetails.alwaysBounceVertical = false
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlstring)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
// print(jsonObj!.value(forKey: "Detail"))
self.itemsArray = (jsonObj!.value(forKey: "Detail") as? [[String: AnyObject]])!
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if (section == 0){
return itemsArray.count
}else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//if indexPath.section == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! productTableViewCell
let arr = itemsArray[indexPath.row]
let urls = NSURL(string: arr["img"] as! String)
let data = NSData (contentsOf: urls! as URL)
cell.imageview.image = UIImage(data: data! as Data)
cell.nameLabel.text = arr["productName"] as? String
var price = arr["productPrice"] as! String
var aQuntity : Float = 1
let itemId : Int = arr["sku"] as! Int
for aDic in CartArray{
if aDic["id"] == String(itemId){
aQuntity = Float(String(aDic["quantity"]!))!
}
}
cell.stepper.value = Double(Int(aQuntity))
cell.stepper.tag = indexPath.row
cell.stepper.addTarget(self, action: #selector(stapperValueChange), for:.valueChanged)
price = price.replacingOccurrences(of: "KD", with: "")
cell.priceLabel.text = String(Float(price)! * aQuntity)
cell.quantityTextField.text = String(aQuntity)
cell.price = price
return cell
// }
// else if indexPath.section == 1{
// let cell = tableView.dequeueReusableCell(withIdentifier: "couponcell", for: indexPath) as! CouponTableViewCell
// return cell
// }else {
// let cell = tableView.dequeueReusableCell(withIdentifier: "checkout", for: indexPath) as! checkoutTableViewCell
// return cell
// }
}
func stapperValueChange (stepper : UIStepper){
let index : Int = stepper.tag
let arr = itemsArray[index]
let cell : productTableViewCell = tableDetails.cellForRow(at: IndexPath(row: index, section: 0)) as! productTableViewCell
let value = Float(stepper.value)
cell.quantityTextField.text = String(value)
let x = Float(cell.price)
cell.priceLabel.text = String(Int(x! * value))
let itemId : Int = arr["sku"] as! Int
var aFoundIndex : Int?
var counter : Int = 0
_ = CartArray.filter { (aDic) -> Bool in
if aDic["id"] == String(itemId){
aFoundIndex = counter
}
counter += 1
return false
}
if(aFoundIndex != nil){
CartArray.remove(at: aFoundIndex!)
}
CartArray.append(["quantity" : String(value),"id" : String(itemId)])
}
here is the image i had designed
这是 UITableviewCell
的代码,在此我放置了步进动作方法来触发,但是当我点击 stepper
时无法更新价格标签,有人能帮我吗?
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper!
@IBOutlet var imageview: UIImageView!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var quantityTextField: UITextField!
var pricearr = [String]()
var price : String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBAction func changeCart(_ sender: Any) {
let value = Int(stepper.value)
quantityTextField.text = String(value)
}
}
这是 UITableViewCell
的代码,在此我放置了步进器动作方法来触发,但是当我点击步进器时无法更新价格标签,有人能帮我吗?
@IBOutlet var tableDetails: UITableView!
var productsArray = [String]()
var nameArray = [String]()
var priceArray = [String]()
let urlstring = "http://www.json-generator.com/api/json/get/ceghMiWudK?indent=2"
var price = [String]()
var sum = 0
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
tableDetails.delegate = self
tableDetails.dataSource = self
tableDetails.alwaysBounceVertical = false
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlstring)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "Detail"))
if let detailsArray = jsonObj!.value(forKey: "Detail") as? NSArray {
for item in detailsArray {
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "productName"){
self.nameArray.append(name as! String)
}
if let price = detailDict.value(forKey: "productPrice"){
self.priceArray.append(price as! String)
}
if let image = detailDict.value(forKey: "img"){
self.productsArray.append(image as! String)
}
}
}
}
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
for item in self.priceArray{
let endIndex = item.index(item.endIndex, offsetBy: -5)
let truncated = item.substring(to: endIndex)
self.price.append(truncated)
}
print(self.price)
})
}
}).resume()
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if (section == 0){
return productsArray.count
}else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! productTableViewCell
let arr = self.productsArray[indexPath.row]
let urls = NSURL(string: arr)
let data = NSData (contentsOf: urls! as URL)
cell.imageview.image = UIImage(data: data! as Data)
cell.nameLabel.text = nameArray[indexPath.row]
let x = priceArray[indexPath.row]
let array = String(x)
cell.priceLabel.text = array
cell.quantityTextField.text = "1"
cell.pricearr = [price[indexPath.row]]
return cell
}else if indexPath.section == 1{
let cell = tableView.dequeueReusableCell(withIdentifier: "couponcell", for: indexPath) as! CouponTableViewCell
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "checkout", for: indexPath) as! checkoutTableViewCell
return cell
}
}
我认为问题出在下面这行
priceLabel.text = x * String(value)
你想在这里完成什么?这不应该编译
如下图所示添加步进器的出口和动作:
将您的代码替换为以下代码:
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper!
@IBOutlet var imageview: UIImageView!
@IBOutlet var nameLabel: UILabel!
@IBOutlet var priceLabel: UILabel!
@IBOutlet var quantityTextField: UITextField!
var pricearr = [String]()
var price : String = ""
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBAction func changeCart(_ sender: Any) {
let value = Int(stepper.value)
quantityTextField.text = String(value)
let x = Int(pricearr[0])
priceLabel.text = String(x! * value)
}
}
Your code has one bug is that when you scroll tableView that time cell will be reset to its default value because of cell reuse.
编辑:
实现目标的更好方法(据我所知)可以定义如下:
- 删除步进动作
从 UITableview 单元格中删除操作 class
class productTableViewCell: UITableViewCell {
@IBOutlet var stepper: UIStepper! @IBOutlet var imageview: UIImageView! @IBOutlet var nameLabel: UILabel! @IBOutlet var priceLabel: UILabel! @IBOutlet var quantityTextField: UITextField! var price : String = "" override func awakeFromNib() { super.awakeFromNib() // Initialization code } }
3.Replace 你的核心逻辑和下面的代码
@IBOutlet var tableDetails: UITableView!
var CartArray : [[String: String]] = []
var itemsArray : [[String: AnyObject]] = []
let urlstring = "http://www.json-generator.com/api/json/get/ceghMiWudK?indent=2"
var sum = 0
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
tableDetails.delegate = self
tableDetails.dataSource = self
tableDetails.alwaysBounceVertical = false
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: urlstring)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
// print(jsonObj!.value(forKey: "Detail"))
self.itemsArray = (jsonObj!.value(forKey: "Detail") as? [[String: AnyObject]])!
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if (section == 0){
return itemsArray.count
}else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//if indexPath.section == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! productTableViewCell
let arr = itemsArray[indexPath.row]
let urls = NSURL(string: arr["img"] as! String)
let data = NSData (contentsOf: urls! as URL)
cell.imageview.image = UIImage(data: data! as Data)
cell.nameLabel.text = arr["productName"] as? String
var price = arr["productPrice"] as! String
var aQuntity : Float = 1
let itemId : Int = arr["sku"] as! Int
for aDic in CartArray{
if aDic["id"] == String(itemId){
aQuntity = Float(String(aDic["quantity"]!))!
}
}
cell.stepper.value = Double(Int(aQuntity))
cell.stepper.tag = indexPath.row
cell.stepper.addTarget(self, action: #selector(stapperValueChange), for:.valueChanged)
price = price.replacingOccurrences(of: "KD", with: "")
cell.priceLabel.text = String(Float(price)! * aQuntity)
cell.quantityTextField.text = String(aQuntity)
cell.price = price
return cell
// }
// else if indexPath.section == 1{
// let cell = tableView.dequeueReusableCell(withIdentifier: "couponcell", for: indexPath) as! CouponTableViewCell
// return cell
// }else {
// let cell = tableView.dequeueReusableCell(withIdentifier: "checkout", for: indexPath) as! checkoutTableViewCell
// return cell
// }
}
func stapperValueChange (stepper : UIStepper){
let index : Int = stepper.tag
let arr = itemsArray[index]
let cell : productTableViewCell = tableDetails.cellForRow(at: IndexPath(row: index, section: 0)) as! productTableViewCell
let value = Float(stepper.value)
cell.quantityTextField.text = String(value)
let x = Float(cell.price)
cell.priceLabel.text = String(Int(x! * value))
let itemId : Int = arr["sku"] as! Int
var aFoundIndex : Int?
var counter : Int = 0
_ = CartArray.filter { (aDic) -> Bool in
if aDic["id"] == String(itemId){
aFoundIndex = counter
}
counter += 1
return false
}
if(aFoundIndex != nil){
CartArray.remove(at: aFoundIndex!)
}
CartArray.append(["quantity" : String(value),"id" : String(itemId)])
}