【问题标题】:How to change tableView numberOfRows and Cells based on which button is clicked in Swift iOS如何根据在 Swift iOS 中单击的按钮更改 tableView numberOfRows 和 Cells
【发布时间】:2021-08-29 10:42:15
【问题描述】:

我的用户个人资料页面中有两个按钮,一个用于保存的商店商品,一个用于他的评论。

我希望当用户点击保存按钮时,它会在表格视图中加载他保存的商店商品,当他点击评论按钮时,它会加载他的评论。

我正在努力弄清楚如何做到这一点

有什么帮助吗?

这是我的代码:

@IBOutlet weak var reviewsBtn: UIButton!
@IBOutlet weak var saveBtntab: UIButton! 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(reviewsBtn.isSelected == true){
            print("review selected")
            return reviews.count
        }
        if(saveBtntab.isSelected == true){
            print("saved selected")
            return shops.count
        }
          return shops.count
      }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

 let cell = tableView.dequeueReusableCell(withIdentifier: "cellFave", for: indexPath) as! FaveTableViewCell
        let shops = self.shops[indexPath.row]
        let reviews = self.reviews[indexPath.row]
// i want to do the same idea for the number of rows here.
}

  @IBAction func reviewsTapped(_ sender: Any) {
        reviewsBtn.isSelected = true
        reviewsBtn.isEnabled = true
        faveBtntab.isEnabled = false
        faveBtntab.isSelected = false
    }
    
    @IBAction func savedTapped(_ sender: Any) {
        faveBtntab.isSelected = true
        faveBtntab.isEnabled = true
        reviewsBtn.isEnabled = false
        reviewsBtn.isSelected = false
      
    }

【问题讨论】:

    标签: ios swift uitableview uibutton


    【解决方案1】:

    首先,如果只有两种状态,您可以简化numberOfRows

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return reviewsBtn.isSelected ? reviews.count : shops.count
    }
    

    cellForRow中做同样的事情,根据reviewsBtn.isSelected显示项目

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
         let cell = tableView.dequeueReusableCell(withIdentifier: "cellFave", for: indexPath) as! FaveTableViewCell
         if reviewsBtn.isSelected {
              let reviews = self.reviews[indexPath.row]
              // assign review values to the UI
         } else {
              let shops = self.shops[indexPath.row]
              // assign shop values to the UI
         }
    }
    

    当状态发生变化时不要忘记致电reloadData

    【讨论】:

      【解决方案2】:

      您可以创建两个不同的数据源实例,以便清晰和分离,如下所示 -

      class ShopsDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
          var shops: [Shop] = []
          var onShopSelected: ((_ shop: Shop) -> Void)?
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return shops.count
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "ShopTableViewCell", for: indexPath) as! ShopTableViewCell
              
              let shop = self.shops[indexPath.row]
              cell.populateDetails(shop: shop)
              
              return cell
          }
      
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              self.onShopSelected?(shops[indexPath.row])
          }
      }
      
      class ReviewsDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
          var reviews: [Review] = []
          var onReviewSelected: ((_ review: Review) -> Void)?
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return reviews.count
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewTableViewCell", for: indexPath) as! ReviewTableViewCell
              
              let review = self.reviews[indexPath.row]
              cell.populateDetails(review: review)
              
              return cell
          }
      
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              self.onReviewSelected?(reviews[indexPath.row])
          }
      }
      
      class ViewController: UIViewController {
          let shopsDataSource = ShopsDataSource()
          let reviewsDataSource = ReviewsDataSource()
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              tableView.register(ShopTableViewCell.self, forCellReuseIdentifier: "ShopTableViewCell")
              tableView.register(ReviewTableViewCell.self, forCellReuseIdentifier: "ReviewTableViewCell")
              
              shopsDataSource.onShopSelected = { [weak self] (shop) in
                  self?.showDetailsScreen(shop: shop)
              }
              
              reviewsDataSource.onReviewSelected = { [weak self] (review) in
                  self?.showDetailsScreen(review: review)
              }
          }
      
          @IBAction func shopsTapped(_ sender: Any) {
              tableView.dataSource = shopsDataSource
              tableView.delegate = shopsDataSource
              tableView.reloadData()
          }
      
          @IBAction func addNewShop(_ sender: Any) {
              /// ask user about shop details and add them here
              shopsDataSource.shops.append(Shop())
              tableView.reloadData()
          }
          
          func showDetailsScreen(shop: Shop) {
              /// Go to shop details screen
          }
          
          @IBAction func reviewsTapped(_ sender: Any) {
              tableView.dataSource = reviewsDataSource
              tableView.delegate = reviewsDataSource
              tableView.reloadData()
          }
      
          @IBAction func addNewReview(_ sender: Any) {
              /// ask user about review details and add them here
              reviewsDataSource.reviews.append(Review())
              tableView.reloadData()
          }
      
          func showDetailsScreen(review: Review) {
              /// Go to review details screen
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2014-06-15
        相关资源
        最近更新 更多