Ios 在UITableViewCell中使用UIStepper增加标签值
我希望能够使用位于每个tableview行中的UIStepper。我希望每个UIStepper更新UIStepper所在的同一tableview行中的标签 我正在尝试的代码如下Ios 在UITableViewCell中使用UIStepper增加标签值,ios,swift,uitableview,uistepper,swift4,Ios,Swift,Uitableview,Uistepper,Swift4,我希望能够使用位于每个tableview行中的UIStepper。我希望每个UIStepper更新UIStepper所在的同一tableview行中的标签 我正在尝试的代码如下 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cartListCell = tableView.dequeueReusableCell(wit
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell
cartListCell?.UI_STEPPER_NAME.value = VALUE_FROM_ARRAY
return cartListCell!
}
@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper) {
// I need to update the value of a label in the tableview cell that tapped
}
更新的实施
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cartListCell?.CartListingProductQtyStepperOutlet.tag = indexPath.row
cartListCell?.CartListingProductQtyStepperOutlet.addTarget(self, action: #selector(self.CartStoreQtyStepperAction(_:)), for: UIControlEvents.valueChanged)
cartListCell?.tag = Int(CartStoreItemIdArray [indexPath.row])!
return cartListCell!
}
@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper)
{
let stepperValue = Int(sender.value)
let indexPath = IndexPath(row: stepperValue, section: 0)
print(stepperValue)
if let cell = yourTableView.cellForRow(at: indexPath) as? CartListingItemTableViewCell
{
print(cell?.tag)
}
}
这样做时,我无法访问tableview单元格和其中的标签。有人能告诉我怎么做吗?在
cellForRowAt
中将标记值分配给UIStepper
的发送者,并使用相同的标记访问cartstoreqtysperaction
操作中的UIStepper
值的更改
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell
//Assign tag value
cartListCell?.UI_STEPPER_NAME.tag = indexPath.row
cartListCell?.UI_STEPPER_NAME.value = VALUE_FROM_ARRAY
cartListCell?.stepper.addTarget(self, action: #selector(self.CartStoreQtyStepperAction(_:)), for: UIControlEvents.valueChanged)
return cartListCell!
}
@IBAction func CartStoreQtyStepperAction(_ sender: UIStepper) {
let index = sender.tag
yourValueArray[index] = sender.value
//Reload particular cell for tableView
}
Swift 4的解决方案
cellforRowAt
中
cartListCell.UI_STEPPER_NAME.tag = indexPath.row
cartListCell.UI_STEPPER_NAME.addTarget(self, action: #selector(CartStoreQtyStepperAction), for: .touchUpInside)
let cartListCell = sender.superview?.superview as! CartListingItemTableViewCell
//check whether your superview is table cell.
//then use the cell instance to update the cell label
sender.tag
访问特定单元格
cartListCell.UI_STEPPER_NAME.text = "\(VALUE_FROM_ARRAY[sender.tag])"
//sender.tag is similar to indexPath.row as we pass the tag value in the cellforRowAt.
//It will work then please accept my answerThis is a solution for Swift 4 using NSKeyValueObservation
- First of all you need a property in your data model to keep the current value of the stepper.
var counter = 0.0
cartListCell.UI\u STEPPER\u NAME.text=“\(数组[sender.tag]中的值)”
//当我们在cellforRowAt中传递标记值时,sender.tag与indexPath.row类似。
//它将起作用,然后请接受我的回答这是使用NSKeyValueObservation
Swift 4的解决方案
- 首先,在数据模型中需要一个属性来保持步进器的当前值
@IBOutlet weak var stepper : UIStepper!
@IBOutlet weak var label : UILabel!
var observation : NSKeyValueObservation?
- 在“自定义”单元中,为步进器和标签创建插座,并为观察创建属性。连接插座
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as! CartListingItemTableViewCell
let item = dataSourceArray[indexPath.row]
cartListCell.stepper.value = item.counter
cartListCell.label.text = "\(item.counter)"
cartListCell.observation = cell.stepper.observe(\.value, options: [.new]) { (_, change) in
cartListCell.label.text = "\(change.newValue!)"
item.counter = change.newValue!
}
return cartListCell
}
- 在
cellForRow
的控制器中添加观察者,dataSourceArray
是数据源数组
// Custom tableview cell code
class CartListingItemTableViewCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var stepper: UIStepper!
}
//视图控制器代码(tableview数据源)
尝试并解决方案2
尽管我能够获得函数的步进值,但我无法将递增函数分配给单元格中的标签检查问题。我已经添加了我试图解决这个问题的实现。我改了it@JobinsJohn-您在此处实现和共享的代码有问题print(cell?.tag)//prints nil
如果let
,则此语句不会在内部执行。正如if-let
确保您的单元格值不为零,如果单元格值为零,则它将不会进入if
块请注意,IndexPath(行:stepperValue,节:0)最有可能是IndexPath(行:stepper.tag,节:0)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cartListCell = tableView.dequeueReusableCell(withIdentifier: reuseCartListingCellIdentifier, for: indexPath) as? CartListingItemTableViewCell
let stepperValue = yourArray[indexPath.row]
cartListCell?.label.text = String(stepperValue) ?? "0"
cartListCell?.stepper.value = Int(stepperValue) ?? 0
cartListCell?.stepper.tag = indexPath.row
cartListCell?.stepper.addTarget(self, action: #selector(self.stepperValueChanged(_:)), for: UIControlEvents.valueChanged)
return cartListCell!
}
// handle stepper value change action
@IBAction func stepperValueChanged(_ stepper: UIStepper) {
let stepperValue = Int(stepper.value)
print(stepperValue) // prints value
let indexPath = IndexPath(row: stepperValue, section: 0)
if let cell = yourTableView.cellForRow(at: indexPath) as? CartListingItemTableViewCell {
cell.label.text = String(stepperValue)
yourValueArray[index] = stepperValue
}
}