繁体   English   中英

根据细分选项重新加载tableview

[英]Reload tableview based on segments options

我正在尝试在用户更改控件段时更改表​​数据。 但是现在我的代码崩溃了。 这是我设置细分的方法:

  let w = view.frame.width
  let s = PinterestSegment(frame: CGRect(x: 20, y: 117, width: w - 40, height: 40), titles: ["Our faviorte", "Restuarnt", "Coffee", "Pharmacy", "Supermarketr", "Home", "Wemoent' Style", "Man's Style", "Beauty", "Travel"])
        s.style.titleFont = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight(rawValue: 5))
        view.addSubview(s)

TableView数据从api保存,并根据细分选项进行过滤。

 var info = Shops(shopname: shopName, Logo: logoString, family_id: familiy_id , design : designImage , rate : rate, time : time )
                        self.shops.append(info)
                  // filters all the segments this way
                        self.coffee = self.shops.filter { $0.shopname == "Starbucks" }

现在,当在ViewdidLoad中更改分段时:

 s.valueChange = { index in
    if index == 1 {

        print(self.coffee)// it prints the data correctly 
        self.shops = self.coffee
    }

    if index == 2 {
        self.shops = self.Restuarnt

    }
    if index == 3 {
        self.shops = self.Pharmacy

    }
 self.tableView.reloadData()
}        

我收到一个fatal error: Index out of range

数据源和委托:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if searchActive {
            return auxiliar!.count
        }else{
            return   allShops.count

        }
    }



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

        if searchActive

        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
            let entry1 = auxiliar?[indexPath.row]
            cell.shopImage.hnk_setImage(from: URL(string: (entry1?.Logo)!))
            cell.shopName.text = entry1?.shopname
            cell.backgroundColor = UIColor(white: 1 , alpha : 0.5)
            cell.time.text = entry1?.time
            cell.star.rating = Double((entry1?.rate)!)
            return cell
        }
        else {

            let shop = allShops[indexPath.row]

            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell

            let entry = shops[indexPath.row]

            cell.shopImage.hnk_setImage(from: URL(string: entry.Logo))
            cell.shopName.text = entry.shopname
            cell.star.rating = Double(entry.rate)
            cell.time.text = entry.time
           cell.backgroundColor = UIColor(white: 1 , alpha : 0.5)
            return cell

        }
    }

检查UITableView的委托和数据源: numberOfRowsInSectioncellForRowAtIndexPath 您的数据源( self.Restuarntself.Pharmacy )中有tableView的单元self.Restuarnt引的数据。

首先登录控制台找到崩溃点,它可能是通过tableview数据源方法崩溃的。 确保self.Restuarnt和self.Pharmacy可以正确打印数据。 还有一件事是,您使用了多个If语句 而是使用if-else-if Ladder ,因为一旦索引匹配,就没有必要检查其他索引!

这种方法应该是这样的,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if searchActive {
            return auxiliar!.count
        }else{
            return   self.shops.count

        }
    }

因为每次您在细分上设置self.shops都会改变

您可以将选定的段索引保存在为类全局定义的某个变量中,并在cellForRow索引方法中访问该变量

而且我认为您可以对此行发表评论,因为您在cellForRow索引方法中未使用任何位置

让商店= allShops [indexPath.row]

您可能在行数上犯了一个错误。 每当细分发生变化时,您都需要重新声明tableview。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM