隐藏或删除indexPath中的第一行/单元格

发布于 2025-01-15 16:57:52 字数 4330 浏览 3 评论 0原文

我尝试在 1 处隐藏或启动 indexPath.row。我怎样才能实现这一点?我已经反转了indexPath并在numberOfRowsInSection - 1上进行计数,因此列表从底部开始,但是当我单击列表时,内容仍然没有根据反转的indexPath.row发生变化

这是我的代码

extension PaymentMethodViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return paymentMethod.count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let data = paymentMethod[indexPath.row]
    switch indexPath {
    case indexPathForSelected:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ConstantsIdentifier.paymentMethodCellId,
                                                       for: indexPath) as? PaymentMethodCell else {
                                                        return UITableViewCell()
                                                        
        }
        cell.layoutIfNeeded()
        cell.setNeedsLayout()
        cell.paymentLbl.text = data.paymentGroupTitle
        cell.subLbl.text = selectedPayment.title
        cell.descriptionLabel.text = selectedPayment.description
        cell.phoneNumberTextField.addTarget(self, action: #selector(textFieldDidChange(sender:)), for: .editingChanged)
        cell.phoneNumberTextField.isHidden = selectedPayment.title == ConstantsText.titleOVO ? false : true
        cell.phoneNumberTextField.textContentType = .telephoneNumber
        cell.phoneNumberTextField.keyboardType = .phonePad
        cell.phoneNumberTextField.delegate = self
        selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode ? cell.withSeparator() : cell.removeSeparator()
        cell.rightIcon.image = selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode ? UIImage(named: ConstantsImage.ovalTick) : UIImage(named: ConstantsImage.arrowRight)
        cell.rightIcon.contentMode = .scaleAspectFit
        
        if selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode {
            hideCellDesc(cell, true)
        } else {
            hideCellDesc(cell, false)
        }
        
        let imageUrl = URL(string: selectedPayment.logo?.lowres ?? "")
        if let objectPayment = paramCheckout?["payment"] as? NSDictionary, let ccNumber = objectPayment["cardNumber"] as? String, !ccNumber.isEmpty {
            cell.paymentImg.image = UIImage(named: getImageCC(ccName: getTypeCreditCard(ccNumber: ccInfo?.ccNumber ?? "")))
        } else {
            cell.paymentImg?.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ConstantsImage.placeholder), options: .highPriority, completed: nil)
        }
        return cell
        
    default:
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: ConstantsIdentifier.defaultIndentifier, for: indexPath)
        cell.textLabel?.text = data.paymentGroupTitle
        cell.textLabel?.numberOfLines = 2
        cell.textLabel?.font  = UIFont.karlaRegular
        let image = UIImageView(image: UIImage(named: ConstantsImage.arrowRight))
        cell.accessoryView = image
        cell.selectionStyle = .none
        return cell
    }
    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPathForSelected == indexPath && selectedPayment.title == ConstantsText.titleOVO {
        return Constant.SelectedHeight + 30
    }
    if indexPathForSelected == indexPath && selectedPayment.code == ConstantsPaymentMethod.creditCard {
        return Constant.CellHeight
    }
    return indexPathForSelected == indexPath ? Constant.SelectedHeight + 30 : Constant.CellHeight
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return Constant.HeaderHeight
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return ConstantsText.titlePaymentMethod
}


func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tintColor = UIColor.white
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont.karlaBold
    header.textLabel?.textColor = UIColor.mainBlue
}

}

请帮助我,谢谢

I try to hide or start the indexPath.row at 1. how can i achieved that ?. I already reversed the indexPath and make the count on numberOfRowsInSection - 1, So the list start from the buttom, but when i clicked on the list the content still not changed according to the reversed indexPath.row

Here is my code

extension PaymentMethodViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return paymentMethod.count
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let data = paymentMethod[indexPath.row]
    switch indexPath {
    case indexPathForSelected:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ConstantsIdentifier.paymentMethodCellId,
                                                       for: indexPath) as? PaymentMethodCell else {
                                                        return UITableViewCell()
                                                        
        }
        cell.layoutIfNeeded()
        cell.setNeedsLayout()
        cell.paymentLbl.text = data.paymentGroupTitle
        cell.subLbl.text = selectedPayment.title
        cell.descriptionLabel.text = selectedPayment.description
        cell.phoneNumberTextField.addTarget(self, action: #selector(textFieldDidChange(sender:)), for: .editingChanged)
        cell.phoneNumberTextField.isHidden = selectedPayment.title == ConstantsText.titleOVO ? false : true
        cell.phoneNumberTextField.textContentType = .telephoneNumber
        cell.phoneNumberTextField.keyboardType = .phonePad
        cell.phoneNumberTextField.delegate = self
        selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode ? cell.withSeparator() : cell.removeSeparator()
        cell.rightIcon.image = selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode ? UIImage(named: ConstantsImage.ovalTick) : UIImage(named: ConstantsImage.arrowRight)
        cell.rightIcon.contentMode = .scaleAspectFit
        
        if selectedPayment.code == ConstantsPaymentMethod.defaultPaymentCode {
            hideCellDesc(cell, true)
        } else {
            hideCellDesc(cell, false)
        }
        
        let imageUrl = URL(string: selectedPayment.logo?.lowres ?? "")
        if let objectPayment = paramCheckout?["payment"] as? NSDictionary, let ccNumber = objectPayment["cardNumber"] as? String, !ccNumber.isEmpty {
            cell.paymentImg.image = UIImage(named: getImageCC(ccName: getTypeCreditCard(ccNumber: ccInfo?.ccNumber ?? "")))
        } else {
            cell.paymentImg?.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: ConstantsImage.placeholder), options: .highPriority, completed: nil)
        }
        return cell
        
    default:
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: ConstantsIdentifier.defaultIndentifier, for: indexPath)
        cell.textLabel?.text = data.paymentGroupTitle
        cell.textLabel?.numberOfLines = 2
        cell.textLabel?.font  = UIFont.karlaRegular
        let image = UIImageView(image: UIImage(named: ConstantsImage.arrowRight))
        cell.accessoryView = image
        cell.selectionStyle = .none
        return cell
    }
    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPathForSelected == indexPath && selectedPayment.title == ConstantsText.titleOVO {
        return Constant.SelectedHeight + 30
    }
    if indexPathForSelected == indexPath && selectedPayment.code == ConstantsPaymentMethod.creditCard {
        return Constant.CellHeight
    }
    return indexPathForSelected == indexPath ? Constant.SelectedHeight + 30 : Constant.CellHeight
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return Constant.HeaderHeight
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return ConstantsText.titlePaymentMethod
}


func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    view.tintColor = UIColor.white
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont.karlaBold
    header.textLabel?.textColor = UIColor.mainBlue
}

}

Please help me, Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2025-01-22 16:57:52

首先添加UITableViewDelegate,我认为这就是问题所在:

extension PaymentMethodViewController: UITableViewDataSource, UITableViewDelegate {}

并且

 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.dataSource = self
     tableView.delegate = self
 }

Firstly add UITableViewDelegate, I think this is the problem:

extension PaymentMethodViewController: UITableViewDataSource, UITableViewDelegate {}

and

 override func viewDidLoad() {
     super.viewDidLoad()
     tableView.dataSource = self
     tableView.delegate = self
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文