2016-05-14 181 views
0

我在swift中使用Array加載我的UITableView。我想要做的是在表已經加載我的數組應該是空的(想要刪除陣列中的所有對象,然後它加載另一個數據集來加載另一個表視圖)如何知道UITableView已經完成加載數據swift

我想要做的是添加幾個UItables dinamically到UIScrollView並且最初將所有數據加載到每個UITableView。然後用戶可以水平滾動滾動視圖並查看其他表。因此,我正在做這樣的事情。

for i in 0..<dm.TableData.count { 

     self.catID=self.dm.TableData[i]["term_id"] as? String 
     self.jsonParser() 
    } 

那麼這是我的jsonParser

func jsonParser() { 



    let urlPath = "http://www.liveat8.lk/mobileapp/news.php?" 
    let category_id=catID 
    let catParam="category_id" 
    let strCatID="\(catParam)=\(category_id)" 

    let strStartRec:String=String(startRec) 
    let startRecPAram="start_record_index" 
    let strStartRecFull="\(startRecPAram)=\(strStartRec)" 


    let strNumOfRecFull="no_of_records=10" 

    let fullURL = "\(urlPath)\(strCatID)&\(strStartRecFull)&\(strNumOfRecFull)" 
    print(fullURL) 


    guard let endpoint = NSURL(string: fullURL) else { 
     print("Error creating endpoint") 
     return 
    } 

    let request = NSMutableURLRequest(URL:endpoint) 
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in 
     do { 
      guard let data = data else { 
       throw JSONError.NoData 
      } 
      guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary else { 
       throw JSONError.ConversionFailed 
      } 
      print(json) 
      if let countries_list = json["data"] as? NSArray 
      { 

       for (var i = 0; i < countries_list.count ; i++) 
       { 
        if let country_obj = countries_list[i] as? NSDictionary 
        { 
         //self.TableData.append(country_obj) 
         self.commonData.append(country_obj) 


         } 
       } 

       //self.updateUI() 
       if self.commonData.isEmpty 
       { 

       } 
       else 
       { 
        self.updateUI() 
       } 



      } 

      } catch let error as JSONError { 
      print(error.rawValue) 
     } catch let error as NSError { 
      print(error.debugDescription) 
     } 
     }.resume() 


} 

然後UpdateUI()

func updateUI() 
{ 


    print("COMMON DATA ARRAY\(self.commonData)") 
    // print("NEWS DATA ARRAY\(self.newsNews)") 
    //print("SPORTS DATA ARRAY\(self.sportsNews)") 
    let tblY:CGFloat=segmentedControl.frame.origin.y+segmentedControl.frame.size.height 
    tblNews=UITableView.init(frame: CGRectMake(x,0 , self.screenWidth, self.screenHeight-tblY)) 
    tblNews.tag=index 
    tblNews.delegate=self 
    tblNews.dataSource=self 
    tblNews.backgroundColor=UIColor.blueColor() 

    self.mainScroll.addSubview(tblNews) 
    x=x+self.screenWidth 




    index=index+1 

    tblNews.reloadData() 


} 

`UITableView` use this `commonData` array as the data source. Now when I scroll table view data load with previous data too.So what is the best way to do this? or else please tell me how can use `self.commonData.removeAll()` after 1 `UITableView` has loaded.Currently I did in `CellforrowAtIndex` 

if indexPath.row == self.commonData.count-1 
    { 
     self.commonData.removeAll() 
    } 


    return cell 

,但這並沒有解決我的問題

+1

你這樣做是錯的。每個表視圖都需要自己的數據。除非這些表格用於顯示完全相同的數據,否則不能重複使用一個數組用於多個表格。 – rmaddy

回答

1

你應該有數據,有可能陣列單獨設置,爲每個UITableView。 iOS會回調您的數據源委託方法來請求數據。

重要的是你不要而不是從數組中刪除數據,因爲iOS會調用數據源委託方法期待數據。即使您最初在表格視圖中顯示數據,用戶也可能滾動滾動視圖,導致其中一個UITableView調用委託方法重新獲取數據。

數據源委託方法(例如func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell)具有UITableView參數,您可以使用該參數來確定哪個數據源適合。

例如,你可能有:

self.commonData1 = ["a", "b", "c"] 
self.commonData2 = ["d", "e", "f"] 

而且你需要跟蹤的任何表添加到您的滾動視圖:

self.tableView1 = ...the table view you create & add to scroll view 
self.tableView2 = ...the table view you create & add to scroll view 

而且當你應對數據源電話:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    if tableView == self.tableView1 { 
     return 1 
    } else if tableView == self.tableView2 { 
     return 1 
    } 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.tableView1 { 
     return self.commonData1.count 
    } else if tableView == self.tableView2 { 
     return self.commonData2.count 
    } 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! IdeaTableViewCell 
    if tableView == self.tableView1 { 
     cell.textLabel?.text = self.commonData1[indexPath.row] 
    } else if tableView == self.tableView2 { 
     cell.textLabel?.text = self.commonData2[indexPath.row] 
    } 

    return cell 
} 
相關問題