Programming/Swift

테이블뷰 페이지 끝에 다다를 때쯔음 다음 테이블 뷰 데이터 불러오기

ilovecoffee 2015. 5. 27. 16:25

<pre><code class="swift">

    var page = 0
    var searchPage = 0
   
    var loadAble = true
    var loadEnd = false
    var loadSearchEnd = false
    var shopList : NSMutableArray!
    var searchShopList : NSMutableArray!


    func searchAllShopList(keyword:String) {
        loadAble = false
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let process = JSONNetwork()
        var arr = ["manager" : "shop", "method" : "search_shop_all", "uid" : uid, "token": token, "page" : String(searchPage), "latitude" : appDelegate.latitude, "longitude" : appDelegate.longitude, "keyword" : keyword]
        process.delegate = self
        process.jsonProccess(arr, code: NetworkCode().GET_SEARCH_SHOP_ALL)

    }

func searchAllShopList(keyword:String) {
        loadAble = false
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let process = JSONNetwork()
        var arr = ["manager" : "shop", "method" : "search_shop_all", "uid" : uid, "token": token, "page" : String(searchPage), "latitude" : appDelegate.latitude, "longitude" : appDelegate.longitude, "keyword" : keyword]
        process.delegate = self
        process.jsonProccess(arr, code: NetworkCode().GET_SEARCH_SHOP_ALL)
    }



func returnJSON(data: NSDictionary, code: Int) {
        if NetworkCode().GET_SHOP_ALL==code{
            var returnData : NSDictionary = data.valueForKey("data") as! NSDictionary
            if(shopList == nil){
                shopList = returnData.valueForKey("shop_list") as? NSMutableArray
            }else{
                var addListShop = returnData.valueForKey("shop_list") as? NSMutableArray
                if addListShop?.count != 0{
                    shopList.addObject(addListShop!)
                }else{
                    loadEnd = true
                }
               
            }
           
            page++
            NSOperationQueue.mainQueue().addOperationWithBlock {
                self.tableView.reloadData()//테이블뷰 재로딩
                self.loadAble = true
            }



if NetworkCode().GET_SHOP_ALL==code{
            var returnData : NSDictionary = data.valueForKey("data") as! NSDictionary
            if(shopList == nil){
                shopList = returnData.valueForKey("shop_list") as? NSMutableArray
            }else{
                var addListShop = returnData.valueForKey("shop_list") as? NSMutableArray
                if addListShop?.count != 0{
                    shopList.addObject(addListShop!)
                }else{
                    loadEnd = true
                }
               
            }
           
            page++
            NSOperationQueue.mainQueue().addOperationWithBlock {
                self.tableView.reloadData()//테이블뷰 재로딩
                self.loadAble = true
            }
        else if NetworkCode().GET_SEARCH_SHOP_ALL==code {
            var returnData : NSDictionary = data.valueForKey("data") as! NSDictionary
            if(searchShopList == nil){
                searchShopList = returnData.valueForKey("shop_list") as? NSMutableArray
            }else{
                var addListShop = returnData.valueForKey("shop_list") as? NSMutableArray
                if addListShop?.count != 0{
                    searchShopList.addObject(addListShop!)
                }else{
                    loadSearchEnd = true
                }
               
            }
           
            searchPage++
            NSOperationQueue.mainQueue().addOperationWithBlock {
                self.searchDisplayController?.searchResultsTableView.reloadData()//SearchBar 결과테이블뷰 재로딩
                self.loadAble = true
            }
        }


   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        var cell : MainTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("MainTableViewCell") as! MainTableViewCell
        var cellData : NSDictionary!
        if(tableView == self.searchDisplayController!.searchResultsTableView){
            cellData = searchShopList[indexPath.row] as! NSDictionary
        }else{
            cellData = shopList[indexPath.row] as! NSDictionary
        }

   func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
       
        if(tableView == self.searchDisplayController!.searchResultsTableView){//검색 결과시
            if(searchShopList.count == (indexPath.row+1)){
                if(loadAble && !loadSearchEnd){
                    searchAllShopList(searchBar.text)
                }
            }
        }else{//기본
            if(shopList.count == (indexPath.row+1)){
                if(loadAble && !loadEnd){
                    getAllShopList()
                }
            }
        }
       
    }
</code></pre>