在显示数据表视图中使用UISegmentedControl

时间:2019-05-17 10:11:34

标签: swift

enter image description here enter image description here““我正在使用细分控件,我有两个细分,一个细分(食谱)和第二个细分(餐厅),请点击i,然后点击第一个细分,数据显示第一个数组,第二段点击第二段,数据显示第二段”

var dataArray = [FollowedData]()
var restdataArray = [RestaurantFollowed]()

   @IBAction func btnActionSegmnet(_ sender: UISegmentedControl) {
        switch (segmentSelect.selectedSegmentIndex) {
        case 0:
            dataArray.removeAll()
            ShowPostRecipesData()
            tableView.reloadData()
            break
        case 1:
            restdataArray.removeAll()
            ShowPostRestaurantsData()
            tableView.reloadData()
            break
        default:
            break
        }
    }
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch (segmentSelect.selectedSegmentIndex) {
    case 0:
     return dataArray.count

    case 1:
      return restdataArray.count

    default:
        return 0
    }
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 430
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "FollowedCell")as! FollowedCell
  switch (segmentSelect.selectedSegmentIndex)
    case 0:

        cell.tittleLbl.text = dataArray[indexPath.row].postDetailsData.title
        cell.descriptionLbl.text = dataArray[indexPath.row].postDetailsData.descriptions
        let date =  getDateFromTime(timeStanp: dataArray[indexPath.row].mbersdata.created)

        cell.showDateLbl.text = date  +  "/ "  + dataArray[indexPath.row].postDetailsData.viewscount  +  "views "
        let urlString = "https://myfoodtalk.com:3001/storage/posts/\(dataArray[indexPath.row].postDetailsData.image1)"
        let escapedString1 = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(escapedString1!)
        if let imgurls = URL(string: escapedString1!) {
            print(imgurls)
            cell.recipesImage.sd_setImage(with:imgurls, placeholderImage: UIImage(named:"placeholder"))
        }
        let urlStrings = "https://myfoodtalk.com:3001/storage/members/\(dataArray[indexPath.row].mbersdata.photo)"
        let escapedStrings = urlStrings.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(escapedStrings!)
        if let imgurl = URL(string: escapedStrings!) {
            print(imgurl)
            cell.userImage.sd_setImage(with:imgurl, placeholderImage: UIImage(named:"placeholder"))
        }
        break
    case 1:
        cell.tittleLbl.text = restdataArray[indexPath.row].restaurantDetailsData.title
        cell.descriptionLbl.text = restdataArray[indexPath.row].restaurantDetailsData.descriptions
        let dates =  getDateFromTime(timeStanp: restdataArray[indexPath.row].created)

        cell.showDateLbl.text = dates  +  "/ "  + restdataArray[indexPath.row].restaurantDetailsData.viewscount  +  "views "
        let urls = "https://myfoodtalk.com:3001/storage/members/\(restdataArray[indexPath.row].mbersdata.photo)"
        let escapedString2 = urls.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(escapedString2!)
        if let imgurls = URL(string: escapedString2!) {
            print(imgurls)
            cell.userImage.sd_setImage(with:imgurls, placeholderImage: UIImage(named:"placeholder"))
        }
        let url = "https://myfoodtalk.com:3001/storage/restaurants/\(restdataArray[indexPath.row].restaurantDetailsData.image1)"
        let escapedStrings3 = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print(escapedStrings3!)
        if let imgurl = URL(string: escapedStrings3!) {
            print(imgurl)
            cell.recipesImage.sd_setImage(with:imgurl, placeholderImage: UIImage(named:"placeholder"))
        }

        break
    default:
        break
    }

  return cell
}

func Sh​​owPostRecipesData(){

    let accessToken = UserDefaults.standard.value(forKey: "access_token")as! String
    let userid = UserDefaults.standard.value(forKey: "userId")as! String
    var mainString = ""
    var url = ""

        mainString = """
        {"where":{"m_id":"\(userid)"},"include":[{"relation":"posts"},{"relation":"members"}]}
        """
        url = "follow-posts?filter=\(mainString)&access_token=\(accessToken)"


    let escapedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    print(escapedString!)
    FTHelper.requestGETURLArray(escapedString!, isShowHud: true, headers: nil, success:
        { (response) in
            let dataArray = response
            if dataArray.isEmpty{
                _ = presentAlertWithOptions("", message: "You have no recipes data ", controller: self, buttons: ["OK"], tapBlock:
                    {
                        (UIAlertAction, position) in
                        if position == 0{

                        }
                })
            }else{

                let followObj  = FollowedData()
                self.dataArray = followObj.getfollowData(dataArray: dataArray)
                self.tableView.reloadData()

            }

    })
    { (error) in
        print(error)
    }

}



func ShowPostRestaurantsData(){

    let accessToken = UserDefaults.standard.value(forKey: "access_token")as! String
    let mid = UserDefaults.standard.value(forKey: "userId")as! String

    var mainString = ""
    var url = ""
    mainString = """
    {"where":{"m_id": "\(mid)"},"include":[{"relation":"restaurants"},{"relation":"members"}]}
    """
    url = "follow-restaurants?filter=\(mainString)&access_token=\(accessToken)"


    let escapedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    print(escapedString!)
    FTHelper.requestGETURLArray(escapedString!, isShowHud: true, headers: nil, success:
        { (response) in
            let dataArray = response
            if dataArray.isEmpty{
                _ = presentAlertWithOptions("", message: "You have no recipes data ", controller: self, buttons: ["OK"], tapBlock:
                    {
                        (UIAlertAction, position) in
                        if position == 0{

                        }
                })
            }else{

                let followObj  = RestaurantFollowed()
                self.restdataArray = followObj.getfollowData(dataArray: dataArray)
                self.tableView.reloadData()

            }

    })
    { (error) in
        print(error)
    }

}

我点击了第一个细分受众群数据,但没有显示在表格视图中

0 个答案:

没有答案