Swift 5.3 UITableView — обновление массива внутри и объекта с помощью UIStepper

Итак, у меня есть UITableView, который заполнен массивом пользовательских объектов Kratom. В объекте Kratom у меня есть свойство, называемое размерами. это массив Int. Каждый элемент в объекте Kratom — это отдельная секция. Тогда каждый элемент свойства размеров является строкой.

Каждая ячейка будет иметь UIStepper, которая может изменять значения размеров. Я заставил его работать тогда, когда я просто использую и Int.

Я не уверен, как получить доступ к массиву размеров в пользовательской ячейке и обновить значения.

Приведенный ниже код — это реализация, которую я использовал перед использованием объекта Kratom. Он использует объект Product, который я хотел обновить до Kratom для других целей. Продукт:

class Product {
    var qty: Int
    
    init(newQty: Int) {
        qty = newQty
    }
}

Инвенторитаблевиевцелл:

    var productQty: Product! {
        didSet {
            inventoryStepper.value = Double(productQty.qty)
            inventoryLbl.text = String(productQty.qty)
        }
    }
    
    @IBAction func stepperAction(_ sender: UIStepper) {
        productQty.qty = Int(sender.value)
        self.inventoryLbl.text = String(Int(sender.value))
    }

Вьюконтроллер:

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "inventoryCell", for: indexPath) as! InventoryTableViewCell
        if productTypes.selectedSegmentIndex == 0 {
            if indexPath.section == 0 || indexPath.section == 2 {
                cell.itemLbl.text = capsules[indexPath.row]
            } else {
                cell.itemLbl.text = productSize[indexPath.row]
            }
            
            cell.productQty = redValues[indexPath.section][indexPath.row]
        } else if productTypes.selectedSegmentIndex == 1 {
            cell.itemLbl.text = productSize[indexPath.row]
            cell.productQty = whiteValues[indexPath.section][indexPath.row]
        } else if productTypes.selectedSegmentIndex == 2 {
            if indexPath.section == 3 || indexPath.section == 5 {
                cell.itemLbl.text = capsules[indexPath.row]
            } else {
                cell.itemLbl.text = productSize[indexPath.row]
            }
            
            cell.productQty = greenValues[indexPath.section][indexPath.row]
        } else if productTypes.selectedSegmentIndex == 3 {
            if indexPath.section == 5 {
                cell.itemLbl.text = productSize[1]
            } else {
                cell.itemLbl.text = productSize[indexPath.row]
            }
            
            cell.productQty = otherValues[indexPath.section][indexPath.row]
        } else if productTypes.selectedSegmentIndex == 4 {
            if indexPath.section == 0 {
                cell.itemLbl.text = sanitizer[indexPath.row]
            } else if indexPath.section == 1 {
                cell.itemLbl.text = hemp[indexPath.row]
            } else if indexPath.section == 2 {
                cell.itemLbl.text = other[indexPath.row]
            }
            
            cell.productQty = specialtyValues[indexPath.section][indexPath.row]
        }
        
        return cell
    }

Для этой цели. Предположим, что Kratom — это то же самое, что и Product, за исключением того, что qty теперь является размерами и теперь является [Int], а не Int.

Я не знаю, как заставить InventoryTableViewCell увидеть, что такое indexPath для нажимаемого UIStepper.


person Micheal    schedule 01.01.2021    source источник
comment
Связанный: stackoverflow .com/questions/49487730/   -  person vadian    schedule 02.01.2021