Ios 更新UICollectionView中的标签
当我单击Ios 更新UICollectionView中的标签,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,当我单击Add(+)或Remove(-)按钮时,我使用UICollectionView创建了这个标签,然后我想更新标签。我怎样才能做到这一点 MainViewController.swift func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if(collectionView == Produc
Add(+)
或Remove(-)
按钮时,我使用UICollectionView创建了这个标签,然后我想更新标签。我怎样才能做到这一点
MainViewController.swift
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == ProductCollectionView){
let Productcell = self.ProductCollectionView.dequeueReusableCell(withReuseIdentifier:"ProductCollectionViewCell", for: indexPath) as! ProductCollectionViewCell
Productcell.AddOrMinusLabel.text = "\(bestOfferProductCount[indexPath.row])"
Productcell.ProdName.text = bestOfferProductName[indexPath.row]
Productcell.ProdPrice.text = bestOfferProductPrice[indexPath.row]
Productcell.ProdtOrgPrice.text = bestOfferProductOriginalPrice[indexPath.row]
Productcell.ProdWeight.text = bestOfferProductWeight[indexPath.row]+" "+bestOfferProductUnit[indexPath.row]
Productcell.ProductImage.setImageFromURl(stringImageUrl: bestOfferProductImage[indexPath.row])
return Productcell
}
}
import UIKit
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
}
ProductCollectionViewCell.swift
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == ProductCollectionView){
let Productcell = self.ProductCollectionView.dequeueReusableCell(withReuseIdentifier:"ProductCollectionViewCell", for: indexPath) as! ProductCollectionViewCell
Productcell.AddOrMinusLabel.text = "\(bestOfferProductCount[indexPath.row])"
Productcell.ProdName.text = bestOfferProductName[indexPath.row]
Productcell.ProdPrice.text = bestOfferProductPrice[indexPath.row]
Productcell.ProdtOrgPrice.text = bestOfferProductOriginalPrice[indexPath.row]
Productcell.ProdWeight.text = bestOfferProductWeight[indexPath.row]+" "+bestOfferProductUnit[indexPath.row]
Productcell.ProductImage.setImageFromURl(stringImageUrl: bestOfferProductImage[indexPath.row])
return Productcell
}
}
import UIKit
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
}
有多种方法可以做到这一点 1.旧的委托机制 当您将单元格出列时,必须删除AllObserver,然后将目标从单元格添加到您的ViewController,以获取带发送方正负号的TapeEvents。在被调用方方法中,必须获取其superView(直到到达collectionViewCell视图),然后从UICollectionView请求该单元格的itemIndex,并相应地更新视图、单元格和模型
class CustomCell: UICollectionCell {
var plusAction: (() -> Void)?
var minusAction: (() -> Void)?
@IBOutlet var counterLabel: UILabel!
@IBAction plusTapped() {
plusAction?()
}
@IBAction minusTapped() {
minusAction?()
}
}
2.使用闭包
您可以在单元格中定义两个闭包,一个用于加号,一个用于减号,而不是将目标添加到viewController,将目标添加到单元格并调用相应的闭包,每次尝试返回单元格时,设置这些闭包并相应地对其执行操作
class CustomCell: UICollectionCell {
var plusAction: (() -> Void)?
var minusAction: (() -> Void)?
@IBOutlet var counterLabel: UILabel!
@IBAction plusTapped() {
plusAction?()
}
@IBAction minusTapped() {
minusAction?()
}
}
在cellDequeue方法中:
cellForItemAtIndexPath(/*...other parameters...*/) -> UICollectionCell {
let cell = collectionView.dequeueCell(/*...other parameters...*/) as! CustomCell
// other customizations
// weak allocation in closure might not be needed here since closure reference is nullable on the other end, but i wrote it just in-case.
cell.plusAction = { [weak weakCell = cell] in
let counter = 0 // i said 0 for ex, You actually have to have them stored somewhere (even initial zeros) and fetch the actual value for this indexPath
let newCounter = counter + 1
// update your Model with newCounter
weakCell?.counterLabel.text = "\(newCounter)"
}
cell.minusAction = { [weak weakCell = cell] in
let counter = 0 // i said 0 for ex, You actually have to have them stored somewhere (even initial zeros) and fetch the actual value for this indexPath
let newCounter = counter - 1
// update your Model with newCounter
weakCell?.counterLabel.text = "\(newCounter)"
}
}
有多种方法可以做到这一点 1.旧的委托机制 当您将单元格出列时,必须删除AllObserver,然后将目标从单元格添加到您的ViewController,以获取带发送方正负号的TapeEvents。在被调用方方法中,必须获取其superView(直到到达collectionViewCell视图),然后从UICollectionView请求该单元格的itemIndex,并相应地更新视图、单元格和模型
class CustomCell: UICollectionCell {
var plusAction: (() -> Void)?
var minusAction: (() -> Void)?
@IBOutlet var counterLabel: UILabel!
@IBAction plusTapped() {
plusAction?()
}
@IBAction minusTapped() {
minusAction?()
}
}
2.使用闭包
您可以在单元格中定义两个闭包,一个用于加号,一个用于减号,而不是将目标添加到viewController,将目标添加到单元格并调用相应的闭包,每次尝试返回单元格时,设置这些闭包并相应地对其执行操作
class CustomCell: UICollectionCell {
var plusAction: (() -> Void)?
var minusAction: (() -> Void)?
@IBOutlet var counterLabel: UILabel!
@IBAction plusTapped() {
plusAction?()
}
@IBAction minusTapped() {
minusAction?()
}
}
在cellDequeue方法中:
cellForItemAtIndexPath(/*...other parameters...*/) -> UICollectionCell {
let cell = collectionView.dequeueCell(/*...other parameters...*/) as! CustomCell
// other customizations
// weak allocation in closure might not be needed here since closure reference is nullable on the other end, but i wrote it just in-case.
cell.plusAction = { [weak weakCell = cell] in
let counter = 0 // i said 0 for ex, You actually have to have them stored somewhere (even initial zeros) and fetch the actual value for this indexPath
let newCounter = counter + 1
// update your Model with newCounter
weakCell?.counterLabel.text = "\(newCounter)"
}
cell.minusAction = { [weak weakCell = cell] in
let counter = 0 // i said 0 for ex, You actually have to have them stored somewhere (even initial zeros) and fetch the actual value for this indexPath
let newCounter = counter - 1
// update your Model with newCounter
weakCell?.counterLabel.text = "\(newCounter)"
}
}
我说,总是在cell类中配置您的cell数据
import UIKit
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var addOrMinusLabel: UILabel!
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
var count = 1 {
didSet{
self.addOrMinusLabel.text = "\(count)"
}
}
func configureData(count: Int) {
self.count = count
}
@IBAction func subtract(_ sender: UIButton) {
self.count -= 1
}
@IBAction func add(_ sender: UIButton) {
self.count += 1
}
}
在您的视图控制器中
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == ProductCollectionView){
let Productcell = self.ProductCollectionView.dequeueReusableCell(withReuseIdentifier:"ProductCollectionViewCell", for: indexPath) as! ProductCollectionViewCell
Productcell.configureData(count: (bestOfferProductCount[indexPath.row]))
// do try to configure these below labels also within cell class
Productcell.ProdName.text = bestOfferProductName[indexPath.row]
Productcell.ProdPrice.text = bestOfferProductPrice[indexPath.row]
Productcell.ProdtOrgPrice.text = bestOfferProductOriginalPrice[indexPath.row]
Productcell.ProdWeight.text = bestOfferProductWeight[indexPath.row]+" "+bestOfferProductUnit[indexPath.row]
Productcell.ProductImage.setImageFromURl(stringImageUrl: bestOfferProductImage[indexPath.row])
return Productcell
}
}
我说,总是在cell类中配置您的cell数据
import UIKit
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var addOrMinusLabel: UILabel!
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
var count = 1 {
didSet{
self.addOrMinusLabel.text = "\(count)"
}
}
func configureData(count: Int) {
self.count = count
}
@IBAction func subtract(_ sender: UIButton) {
self.count -= 1
}
@IBAction func add(_ sender: UIButton) {
self.count += 1
}
}
在您的视图控制器中
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == ProductCollectionView){
let Productcell = self.ProductCollectionView.dequeueReusableCell(withReuseIdentifier:"ProductCollectionViewCell", for: indexPath) as! ProductCollectionViewCell
Productcell.configureData(count: (bestOfferProductCount[indexPath.row]))
// do try to configure these below labels also within cell class
Productcell.ProdName.text = bestOfferProductName[indexPath.row]
Productcell.ProdPrice.text = bestOfferProductPrice[indexPath.row]
Productcell.ProdtOrgPrice.text = bestOfferProductOriginalPrice[indexPath.row]
Productcell.ProdWeight.text = bestOfferProductWeight[indexPath.row]+" "+bestOfferProductUnit[indexPath.row]
Productcell.ProductImage.setImageFromURl(stringImageUrl: bestOfferProductImage[indexPath.row])
return Productcell
}
}
试试这个简单的,清醒的 将此代码添加到
viewcontroller
@IBAction func onBtnPlusClick(_ sender : UIButton) {
if let cell = sender.superview?.superview as? ProductCollectionViewCell {
let indexPath = yourCollectionViewOutlet.indexPath(for: cell)
//Do whatever you want or update your data model values
yourCollectionViewOutlet.reloadData()
}
}
@IBAction func onBtnMinusClick(_ sender : UIButton) {
if let cell = sender.superview?.superview as? ProductCollectionViewCell {
let indexPath = yourCollectionViewOutlet.indexPath(for: cell)
//Do whatever you want or update your data model values
yourCollectionViewOutlet.reloadData()
}
}
在您的cellForItemAt
addtargetto按钮中
btnPlus.addTarget(self, action: #selector(self. onBtnPlusClick(_:)), for: .touchUpInside)
btnMinus.addTarget(self, action: #selector(self. onBtnPlusClick(_:)), for: .touchUpInside)
在ProductCollectionViewCell
class中添加两个按钮插座
@IBOutlet var btnPlus : UIButton!
@IBOutlet var btnMinus : UIButton!
就这样,希望这有帮助试试这个简单的n清醒 将此代码添加到
viewcontroller
@IBAction func onBtnPlusClick(_ sender : UIButton) {
if let cell = sender.superview?.superview as? ProductCollectionViewCell {
let indexPath = yourCollectionViewOutlet.indexPath(for: cell)
//Do whatever you want or update your data model values
yourCollectionViewOutlet.reloadData()
}
}
@IBAction func onBtnMinusClick(_ sender : UIButton) {
if let cell = sender.superview?.superview as? ProductCollectionViewCell {
let indexPath = yourCollectionViewOutlet.indexPath(for: cell)
//Do whatever you want or update your data model values
yourCollectionViewOutlet.reloadData()
}
}
在您的cellForItemAt
addtargetto按钮中
btnPlus.addTarget(self, action: #selector(self. onBtnPlusClick(_:)), for: .touchUpInside)
btnMinus.addTarget(self, action: #selector(self. onBtnPlusClick(_:)), for: .touchUpInside)
在ProductCollectionViewCell
class中添加两个按钮插座
@IBOutlet var btnPlus : UIButton!
@IBOutlet var btnMinus : UIButton!
就是这样,希望这有助于我使用协议, 将其添加到单元格的类中,并将iAction添加到UIButton中
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
weak open var delegate: ProductCollectionViewCellDelegate?
@IBAction func btnIncrement(_ sender: UIButton)
{
if let d = self.delegate {
d.didTapOnIncrement(cell: self)
}
}
@IBAction func btnDecrement(_ sender: UIButton)
{
if let d = self.delegate {
d.didTapOnDecrement(cell: self)
}
}
}
protocol ProductCollectionViewCellDelegate: NSObjectProtocol {
func didTapOnIncrement(cell: ProductCollectionViewCell)
func didTapOnDecrement(cell: ProductCollectionViewCell)
}
现在在cellForItemAt方法中,添加以下行-\
cell.delegate = self
现在确认学员确认协议。在本例中,我假设它是您的viewController
extension ViewController: ProductCollectionViewCellDelegate {
func didTapOnIncrement(cell: ProductCollectionViewCell) {
//let indexPath = self.tableView.indexPath(for: cell)
//You have the cell's instance here. You can also get the indexPath by delegate method.
//You can add the increment-decrement logic here.
}
func didTapOnDecrement(cell: ProductCollectionViewCell) {
//let indexPath = self.tableView.indexPath(for: cell)
//You have the cell's instance here. You can also get the indexPath by delegate method.
//You can add the increment-decrement logic here.
}
}
我会使用协议来做这件事, 将其添加到单元格的类中,并将iAction添加到UIButton中
class ProductCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var ProdPrice: UILabel!
@IBOutlet weak var ProdtOrgPrice: UILabel!
@IBOutlet weak var ProductImage: UIImageView!
@IBOutlet weak var ProdName: UILabel!
@IBOutlet weak var ProdWeight: UILabel!
weak open var delegate: ProductCollectionViewCellDelegate?
@IBAction func btnIncrement(_ sender: UIButton)
{
if let d = self.delegate {
d.didTapOnIncrement(cell: self)
}
}
@IBAction func btnDecrement(_ sender: UIButton)
{
if let d = self.delegate {
d.didTapOnDecrement(cell: self)
}
}
}
protocol ProductCollectionViewCellDelegate: NSObjectProtocol {
func didTapOnIncrement(cell: ProductCollectionViewCell)
func didTapOnDecrement(cell: ProductCollectionViewCell)
}
现在在cellForItemAt方法中,添加以下行-\
cell.delegate = self
现在确认学员确认协议。在本例中,我假设它是您的viewController
extension ViewController: ProductCollectionViewCellDelegate {
func didTapOnIncrement(cell: ProductCollectionViewCell) {
//let indexPath = self.tableView.indexPath(for: cell)
//You have the cell's instance here. You can also get the indexPath by delegate method.
//You can add the increment-decrement logic here.
}
func didTapOnDecrement(cell: ProductCollectionViewCell) {
//let indexPath = self.tableView.indexPath(for: cell)
//You have the cell's instance here. You can also get the indexPath by delegate method.
//You can add the increment-decrement logic here.
}
}
使用多个阵列作为数据源是一种可怕的设计。1) 将类用作模型2)将
cellForRow
中的模型项传递给单元格3)在单元格中执行iAction
s,同时进行更改和更新模型。请遵守变量名以小写字母开头的命名约定。我刚刚实现了在collectionView中显示数据。我也不知道该怎么办。只是在寻找解决办法。我没有得到任何与此相关的解决方案。我是ios的初学者development@vadian谢谢我明白你的意思。但是,如果我想添加或删除产品数量,并通过单击+或-按钮向标签显示如何添加或删除产品数量,请查看我之前评论中的3)。如果你有对数据模型的引用,你可以在单元格中做任何事情,假设你保持模型同步。就我个人而言,我不希望单元格直接更新模型。让计算单元实现一个委托协议,以便它可以通知视图控制器+/-点击,然后视图控制器可以更新模型并刷新项目。使用多个数组作为数据源是一个可怕的设计。1) 将类用作模型2)将cellForRow
中的模型项传递给单元格3)在单元格中执行iAction
s,同时进行更改和更新模型。请遵守变量名以小写字母开头的命名约定。我刚刚实现了在collectionView中显示数据。我也不知道该怎么办。只是在寻找解决办法。我没有得到任何与此相关的解决方案。我是ios的初学者development@vadian谢谢我明白你的意思。但是,如果我想添加或删除产品数量,并通过单击+或-按钮向标签显示如何添加或删除产品数量,请查看我之前评论中的3)。如果你有对数据模型的引用,你可以在单元格中做任何事情,假设你保持模型同步。就我个人而言,我不希望单元格直接更新模型。让计算单元实现委派协议,以便它可以将+/-点击通知视图控制器,然后视图控制器可以更新模型并刷新项目。