Tableview没有重新加载数据

时间:2017-05-08 16:54:31

标签: ios swift uitableview alamofire

我遇到了这个小问题,我有一个tableviewcontroller,也是searchresultcontroller。我正在针对每个api调用获取正确的数据,但tableview没有重新加载。我不知道为什么它不起作用。非常感谢任何帮助或领导。

    class MasterViewController: UITableViewController,UISearchResultsUpdating {
    var request:DataRequest?
    var peopleArr:[Peoples] = []

    // MARK: - View Setup
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Search"

        definesPresentationContext = true
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50.0
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            //           if searchController.searchBar.selectedScopeButtonIndex == 0 {
            let profileVc = self.storyboard?.instantiateViewController(withIdentifier: "profileVc") as! ProfileController
            profileVc.profileData = (peopleArr[indexPath.row].user_id, peopleArr[indexPath.row].user_id)
            self.navigationController?.pushViewController(profileVc, animated: true)
        }
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return peopleArr.count
    }
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return tableView.dequeueReusableCell(withIdentifier: "headerPeopleSec")
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "People"
    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingsCell", for: indexPath) as! FollowingsCell
        cell.textLabel?.text = "\(indexPath.row)"
        let people: Peoples
        people = peopleArr[indexPath.row]

        if people.following == "1" {
            cell.followBtn.isHidden = true
        }
        else{
            cell.followBtn.isHidden = false
        }
        cell.profile_thumb!.showImageWithURL(urlString: people.photo_url)
        cell.addAction = { cell in
            self.addFriendAction(indexPath: indexPath , user:people)
        }
        cell.profile_thumb.motionIdentifier = people.user_id
        cell.username.text = people.user_name
        return cell
    }
    func getPeopleList(searchString:String?) {
        if let req = self.request {
            req.cancel()
        }
        let peopleBag = [
            "auth_token": (MemberProfile.loggedUser?._auth_token())!,
            "per_page": 30,
            "page": 1,
            "search_key": searchString ?? ""
            ] as [String : Any]
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
        self.request = HelperClass().doGetRequestCustom(url: BASE_URL + SEARCH_PEOPLE, param:peopleBag, header: [:], completion: {(response,responseObject, error) in

            if let resMsg = (responseObject?.message.resp_status) {
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()

                //                if let hasNext = responseObject?.message.paging_data.next_page_exist as? Bool {
                //                    self.hasNextPage = hasNext
                //                }
                let dictionary:[String: AnyObject]? =  responseObject?.message.data as? [String:AnyObject] //["member_followings"]

                if let dict:Array = dictionary?["member_profiles"] as? Array<[String:AnyObject]>{

                    for dic in dict {
                        let friend = Peoples()
                        friend.photo_url = (dic["photo"] as? String) ?? ""
                        friend.user_name = ((dic["user"]?["username"])! as String)
                        friend.user_id = (dic["id"])! as! String
                        friend.following = (dic["is_following"])! as! String
                        self.peopleArr.append(friend)
                    }
                    self.tableView.reloadData()
                }
                else{
                }
            }
            else{
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
            }

            NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
        })
    }

    func addFriendAction(indexPath:IndexPath , user:Peoples)  {
        let followBag = [
            "auth_token": (MemberProfile.loggedUser?.auth_token)!,
            "following_profile_id":  user.user_id
            ] as [String : Any]
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
        HelperClass().doPostRequest(url: BASE_URL+FOLLOW_MEMBER , param: followBag, completion: { (dataResponse,response,error) in

            if (response != nil) && (response?.message.resp_status)!
            {
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                let cell = self.tableView.cellForRow(at: indexPath) as! FollowingsCell
                cell.followBtn.isHidden = true
                user.following = "1"
            }
            else
            {
                if (response != nil){
                    NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                    HelperClass.showAlertViewWithTitle(title: "Error", Text: (response?.message.message)!, controllerToShowOn: self)
                }
                else{
                    NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                    HelperClass.showAlertViewWithTitle(title: "Error", Text: "Something went wrong. Please check your internet connection & try again later.", controllerToShowOn: self)
                }
                return
            }
        })
    }
    func updateSearchResults(for searchController: UISearchController) {
        if !(searchController.searchBar.text! == "") {
            self.peopleArr.removeAll()
            self.tableView.reloadData()
            let searchBar = searchController.searchBar
            self.getPeopleList(searchString: searchBar.text!)
        }
    }
  }

2 个答案:

答案 0 :(得分:0)

private void notification() { DateTime now = DateTime.Now; if (now.Hour == 10 && now.Minute == 30) { notify.BalloonTipText = "It's 10:30 am"; notify.ShowBalloonTip(3000); timer1.Stop(); } } 似乎有问题。 您可以尝试在此功能结束时移动func updateSearchResults(for searchController: UISearchController)吗? 看来当调用reloadData时,数组已清除,但尚未填充新值。

答案 1 :(得分:0)

您需要在主线程上进行重新加载调用:

#include<stdio.h>
#include<conio.h>
int main()
    {
       char str[100];
       int i;
       printf("Enter a string: ");
       fgets(str,100,stdin); 
       //scanf("%s",str);
       printf("String is: %s\n",str);
       printf("ASCII value in Decimal is: ");
      for(i=0; str[i]!='\0'; i++)
        {
             printf("%d ",str[i]);
          }
          printf("\n");
    getch();
}                                             

所有UI修改始终必须在主线程上进行。大部分时间你都在一个完成处理程序中,你必须调度到main来修改UI。