如果他们将所有Swift用户留在iOS上,那么搜索干净就会离开

时间:2016-04-03 23:12:48

标签: ios swift uitableview uistoryboard

我在主故事板中有一个TableView,用于搜索用户但是当我进入俯视搜索栏的部分时,我加载所有用户而没有我放置任何我想要的东西是删除这些用户和我用户在我开始看不到的时候去,进入用户搜索的部分留下我的UITableViewController代码和图像。

import UIKit
import Parse

class usersVC: UITableViewController, UISearchBarDelegate{

    // declare search bar
    var searchBar = UISearchBar()

    // tableView arrays to hold information from server
    var usernameArray = [String]()
    var avaArray = [PFFile]()




    // default func
    override func viewDidLoad() {
        super.viewDidLoad()

        // implement search bar
        searchBar.delegate = self
        searchBar.sizeToFit()
        searchBar.tintColor = UIColor.groupTableViewBackgroundColor()
        searchBar.frame.size.width = self.view.frame.size.width - 34
        let searchItem = UIBarButtonItem(customView: searchBar)
        self.navigationItem.leftBarButtonItem = searchItem

        // call functions
        loadUsers()

    }



    // SEARCHING CODE
    // load users function
    func loadUsers() {

        let usersQuery = PFQuery(className: "_User")
        usersQuery.addDescendingOrder("createdAt")
        usersQuery.limit = 20
        usersQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
            if error == nil {

                // clean up
                self.usernameArray.removeAll(keepCapacity: false)
                self.avaArray.removeAll(keepCapacity: false)

                // found related objects
                for object in objects! {
                    self.usernameArray.append(object.valueForKey("username") as! String)
                    self.avaArray.append(object.valueForKey("ava") as! PFFile)
                }

                // reload
                self.tableView.reloadData()

            } else {
                print(error!.localizedDescription)
            }
        })

    }


    // search updated
    func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

        // find by username
        let usernameQuery = PFQuery(className: "_User")
        usernameQuery.whereKey("username", matchesRegex: "(?i)" + searchBar.text!)
        usernameQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
            if error == nil {

                // if no objects are found according to entered text in usernaem colomn, find by fullname
                if objects!.isEmpty {

                    let fullnameQuery = PFUser.query()
                    fullnameQuery?.whereKey("username", matchesRegex: "(?i)" + self.searchBar.text!)
                    fullnameQuery?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in
                        if error == nil {

                            // clean up
                            self.usernameArray.removeAll(keepCapacity: false)
                            self.avaArray.removeAll(keepCapacity: false)

                            // found related objects
                            for object in objects! {
                                self.usernameArray.append(object.objectForKey("username") as! String)
                                self.avaArray.append(object.objectForKey("ava") as! PFFile)
                            }

                            // reload
                            self.tableView.reloadData()

                        }
                    })
                }

                // clean up
                self.usernameArray.removeAll(keepCapacity: false)
                self.avaArray.removeAll(keepCapacity: false)

                // found related objects
                for object in objects! {
                    self.usernameArray.append(object.objectForKey("username") as! String)
                    self.avaArray.append(object.objectForKey("ava") as! PFFile)
                }

                // reload
                self.tableView.reloadData()

            }
        })

        return true
    }


    // tapped on the searchBar
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

        // hide collectionView when started search

        // show cancel button
        searchBar.showsCancelButton = true
    }


    // clicked cancel button
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        // unhide collectionView when tapped cancel button

        // dismiss keyboard
        searchBar.resignFirstResponder()

        // hide cancel button
        searchBar.showsCancelButton = false

        // reset text
        searchBar.text = ""

        // reset shown users
        loadUsers()
    }



    // TABLEVIEW CODE
    // cell numb
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return usernameArray.count
    }

    // cell height
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.view.frame.size.width / 4
    }

    // cell config
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // define cell
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! followersCell

        cell.avaImg.image = nil

        // hide follow button
        cell.followBtn.hidden = true

        // connect cell's objects with received infromation from server
        cell.usernameLbl.text = usernameArray[indexPath.row]
        avaArray[indexPath.row].getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in
            if error == nil {
                cell.avaImg.image = UIImage(data: data!)
            }
        }

        return cell
    }


    // selected tableView cell - selected user
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        // calling cell again to call cell data
        let cell = tableView.cellForRowAtIndexPath(indexPath) as! followersCell

        // if user tapped on his name go home, else go guest
        if cell.usernameLbl.text! == PFUser.currentUser()?.username {
            let home = self.storyboard?.instantiateViewControllerWithIdentifier("homeVC") as! homeVC
            self.navigationController?.pushViewController(home, animated: true)
        } else {
            guestname.append(cell.usernameLbl.text!)
            let guest = self.storyboard?.instantiateViewControllerWithIdentifier("guestVC") as! guestVC
            self.navigationController?.pushViewController(guest, animated: true)
        }
    }





    // cell line spasing
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }

    // cell inter spasing
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }



    // scrolled down
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        // scroll down for paging
        if scrollView.contentOffset.y >= scrollView.contentSize.height / 6 {

        }
    }


}

enter image description here

0 个答案:

没有答案