如何处理uitaiteViewCell中按钮的按钮操作?
我正在从事一个购物应用程序项目,客户可以在其中浏览产品并将其添加到购物车中,但是很难弄清楚如何处理TableViewCell中的按钮按钮。我希望它能工作的方式是,按下空圆按钮时,图像将带有选中标记的圆圈更改为填充的圆圈,并且该单元格中的产品被添加到客户的“购物车”中。 “购物车”由两个阵列产品
和数量
组成。
这是表观视图的样子:
这是我到目前为止的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductToBuyCell") as! ProductToBuyCell
//Configure the Selection Button
cell.selectButton.tag = indexPath.row
cell.selectButton.addTarget(self, action: #selector(ItemSelected), for: .touchUpInside)
let product = productsArray[indexPath.row]
//configure cell
cell.productImg.image = product.productPhoto
cell.productImg.contentMode = .scaleAspectFit
cell.productName.text = product.Name
cell.price.text = "$\(product.price)"
return cell
}
// func for when the selectButton is tapped
// tag is equal to indexPath.row
@objc func ItemSelected(sender: UIButton) {
sender.imageView?.image = UIImage(systemName: "checkmark.circle.fill")
let product = productsArray[sender.tag]
newOrder.products.append(product)
newOrder.quantities.append(1)
}
因此,如果有人可以解释如何正确处理由UITATION VIEWCELL中元素引起的事件,以及如何使按钮图像更改,这将不胜感激。
I'm working on a shopping app project where customers can browse through products and add them to their cart, but am having trouble figuring out how to handle button presses within the tableViewCell. They way I want it to work is that when the empty circle button is pressed, the image changes to a filled circle with a checkmark, and the product within that cell is added to the customers "cart". The "cart" consists of two arrays products
and quantities
held within my CustomerOrder object.
Here's what the tableView looks like:
Here's my code so far:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductToBuyCell") as! ProductToBuyCell
//Configure the Selection Button
cell.selectButton.tag = indexPath.row
cell.selectButton.addTarget(self, action: #selector(ItemSelected), for: .touchUpInside)
let product = productsArray[indexPath.row]
//configure cell
cell.productImg.image = product.productPhoto
cell.productImg.contentMode = .scaleAspectFit
cell.productName.text = product.Name
cell.price.text = "$\(product.price)"
return cell
}
// func for when the selectButton is tapped
// tag is equal to indexPath.row
@objc func ItemSelected(sender: UIButton) {
sender.imageView?.image = UIImage(systemName: "checkmark.circle.fill")
let product = productsArray[sender.tag]
newOrder.products.append(product)
newOrder.quantities.append(1)
}
So if someone could please explain how to properly handle events that are caused by elements within a UITableViewCell and how to get the buttons image to change, that would be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
uibutton
'simageView
是只读的 - 要更新其映像,您应该使用setimage
。请注意,您需要跟踪行的选择状态,否则选择状态将在单元重新加载时重置(例如,滚动并回到视图中)。也大概您还需要处理取消按钮的情况。另外,由于您仅在单元格中的
uibutton
上处理选择,因此点击单元格不会对按钮的状态产生任何影响。取而代之的是,您可以实现tableView(_:didSelectrowat:)
并在此处处理选择,并且只有一个简单的uiimageView
而不是uibutton
uibutton 细胞。您也可以考虑使用 。uitaiteView
还具有,因此,如果您将其打开,可以为您跟踪所选的单元格,而您只需要相应地更新单元格的外观即可。至于使用
tag
来找出单元格索引,这是真的,这不是很健壮,但不是问题。绝对值得在上面的评论中查看链接的问题,以了解有关此问题的一些良好讨论。是的,如果您要坚持使用这种方法,则至少应该在添加新的动作之前删除任何现有动作以避免重复事件,例如
button.removetarget(nil,action:nil:nil,for:.Allelevents)
。UIButton
'simageView
is read-only — to update its image you should usesetImage
. Note you'll need to keep track of the selection state of the rows or else the selection state will be reset whenever the cell reloads (e.g. it scrolls out and back into view). Also presumably you'll want to handle the case where the button is deselected.Also, since you're only handling the selection on the
UIButton
in the cell, tapping the cell will not have any effect on the button's state. You could instead implementtableView(_:didSelectRowAt:)
and handle the selection there, and just have a simpleUIImageView
instead of aUIButton
in your cell. You could also consider using theaccessoryView
.UITableView
also has built-in support for multiple selection, so if you turn that on it can keep track of the selected cells for you, and you just need to update the appearance of the cells accordingly.As for using the
tag
to figure out the cell index, it's true it isn't very robust but that wasn't the question. Definitely worth checking out the linked question in the comments above for some good discussion around that.And yes, if you're going to stick with this approach you should at least remove any existing actions before adding a new one to avoid duplicate events, e.g.
button.removeTarget(nil, action: nil, for: .allEvents)
.我假设您的
产品
模型就是这样。跟踪所选产品。您需要保留细胞的状态。创建另一个类似的模型
为 @ paulw11提到的是“
tag
不是一个很好的解决方案,因为默认值为0,如果重新排序行,它将断开”,您可以使用委托模式来获取所选产品指数。创建productTobuycell的协议。
将委托属性添加到单元格中。
还要添加
iboutlet
和ibaction
producttobuycell
中的按钮。然后在单元格中添加SelectionProduct属性,并设置
uilabel
的文本和uiimage
的图像使用属性观察者didset
修改
uiimage的外观
在awakefromnib()
方法中,然后修改
cellforrowat
如下所示。在这里,productSelectionArray
是productsection
不是产品
的数组。现在,确认
委托
producttobuycell
toview> view controller
。在这里,首先,我更改所选单元格的状态,然后使用filter
和map
函数在productsselectionarray
中检索所有选定的产品。然后重新加载行更新UI。根据您的UI,似乎所有选定的产品数量都是一个,因此您不必将另一个数组维护数量。
I assume your
Product
model is like this.To keep track of the selected product. You need to preserve the state of the cell. Create another model like this
As @ Paulw11 mentioned that "
tag
isn't a great solution since the default is 0 and it will break if rows are reordered", you can use delegate pattern to get the selected product index.Create a protocol of the ProductToBuyCell.
Add a delegate property to cell.
Also add an
IBOutlet
andIBAction
of the button inProductToBuyCell
.Then add a selectionProduct property in the cell and set text of
UILabel
and image ofUIImage
using property observerdidSet
Modify the appearence of
UIImage
inawakeFromNib()
methodThen modify the
cellForRowAt
method like below. Here,productSelectionArray
is an array ofProductSection
notProduct
.Now confirm the
delegate
ofProductToBuyCell
toViewController
. Here, at first I change the state of the selected cell and then retrieve all the selected products usingfilter
andmap
function inproductSelectionArray
. And then reload the row to update UI.According to your UI it seems that all the selected products quantity is one, so you don't have to maintain another array for quantity.