2016-05-14 178 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 
} 
相关问题