如何根据在 Swift iOS 中单击哪个按钮更改 tableView numberOfRows 和 Cells

How to change tableView numberOfRows and Cells based on which button is clicked in Swift iOS

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

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

我正在苦苦思索如何做到这一点

有什么帮助吗?

这是我的代码:

@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
      
    }

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

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
    }
}

首先如果只有两个状态你可以简化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