gpt4 book ai didi

ios - 通过点击同一单元格中的 UIButton 访问自定义单元格对象属性 SWIFT

转载 作者:行者123 更新时间:2023-11-29 02:17:18 24 4
gpt4 key购买 nike

我有一个带有加减按钮和数量标签的自定义单元格设置。我希望 View Controller 中的标签能够更新单元格中显示的价格。

我搜索过 SE 和谷歌机器,但所有示例都是针对 Objective-C 的,并且对将 indexPath.row 传递给选择器感到满意。我已经设法将点击添加按钮的行传递给选择器方法,但是我无法访问单元格中的对象,例如 cell.itemNameLabel.text

我尝试在选择器方法中实现类似于 objective-c 的东西:没有任何乐趣。

UITableViewCell* cell = (UITableViewCell*)[sender superview];
NSIndexPath* indexPath = [myTableView indexPathForCell:cell];

这是我的设置:

自定义单元格:

class ProductListTableViewCell: UITableViewCell {

@IBOutlet var itemNameLabel: UILabel! = UILabel()
@IBOutlet weak var itemImage: UIImageView! = UIImageView()
@IBOutlet var itemPriceLabel: UILabel! = UILabel()
@IBOutlet weak var itemQty: UILabel!
@IBOutlet weak var addItem: UIButton! = UIButton()

主视图 Controller :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:ProductListTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProductListTableViewCell

let drink:PFObject = self.productListData.objectAtIndex(indexPath.row) as PFObject

cell.itemImage.layer.cornerRadius = cell.itemImage.frame.size.width / 2
cell.itemImage.clipsToBounds = true

cell.addItem.tag = indexPath.row
cell.addItem.addTarget(self, action:("cellAddItemAction:"), forControlEvents: UIControlEvents.TouchUpInside)

cell.itemNameLabel.text = drink.objectForKey("name") as? NSString
cell.calorieLabel.text = drink.objectForKey("calories") as? NSString

cell.itemPriceLabel.text = drink.objectForKey("price") as? NSString
cell.itemImage.alpha = 1

let itemImage = drink["image"] as PFFile

itemImage.getDataInBackgroundWithBlock{
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
let image = UIImage(data: imageData)
cell.itemImage.image = image
}

}
}
)
return cell
}

选择器方法:

func cellAddItemAction(sender: UIButton){

println("pressed \(sender.tag)")

}

这可行,但我需要更改单元格中的对象,例如 cell.itemQty.text。

希望这很清楚,我对编程很陌生,所以请耐心等待我的术语,我知道这不太对!!感谢您的帮助。

最佳答案

好的,所以我的做法略有不同。这是一个原型(prototype)应用程序,只需要看起来可以工作。如果它对任何人都有帮助,这就是我所做的。

cellForRowAtIndexPath 中为按下的按钮添加标签:

cell.addItem.tag = indexPath.row

然后在添加目标时,不要使用 Self,而是使用单元格:

cell.addItem.addTarget(cell, action:("cellAddItemAction:"), forControlEvents: UIControlEvents.TouchUpInside)

在viewController中声明一个全局变量:

让 priceUpdateKey = "com.ordr.priceUpdateKey"

现在在单元类中编写函数,并添加 NSNotification 方法,在 userInfo 参数中发送您的数据

func cellAddItemAction(sender: UIButton){

var count = (String(self.itemQty.text!)).toInt()!
totalPrice = (self.itemPriceLabel!.text! as NSString).doubleValue

count += 1

NSNotificationCenter.defaultCenter().postNotificationName(priceUpdateKey, object: nil, userInfo: ["totalPrice":totalPrice])

}

然后回到 viewController,为 NSNotification 添加一个观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "updatePrice:", name: priceUpdateKey, object: nil)

最后编写观察者调用的函数:

func updatePrice(sender: NSNotification) {

let userInfo:Dictionary<String,Double!> = sender.userInfo as Dictionary<String,Double!>
let messagePrice = userInfo["totalPrice"]
let finalCalc = totalAmount + messagePrice!
totalAmount = finalCalc

totalPriceButton.setTitle("£\(totalAmount)0", forState: UIControlState.Normal)

}'

注意** 我并不是说这是一个好方法,但它对我有用,如果对你有帮助,那就完美了。

关于ios - 通过点击同一单元格中的 UIButton 访问自定义单元格对象属性 SWIFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28616300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com