[英]How do i scroll a UITable view down until i see a cell with label “Value” in Calabash
[英]i'm not able to update the label when i tapped on stepper which is placed in uitable view cell
这是我设计的图像,这是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)
}
}
您的代码有一个错误是,滚动tableView时,由于单元重用,该时间单元将重置为其默认值。
编辑:
达到目标的更好方法(据我了解)可以定义如下:
从UITableview单元格类中删除操作
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,用下面的代码替换你的核心逻辑
@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)])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.