2016-03-15 65 views
0

如何配置我的tableview以僅反映具有非隱藏單元格的行?這是一個有問題的實現代碼如下視圖控制器代碼:在UITableViewCell中隱藏行

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let post = posts[indexPath.row] 
    if let cell = tableView.dequeueReusableCellWithIdentifier("favorCell") as? FavorCell { 
      cell.configureCell(post) 
      cell.hidePost(post) 
      return cell 
    } else { 
     return FavorCell() 
    } 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return posts.count 
} 

這是我TableViewCell函數隱藏單元格:

func hidePost(post: Post){ 
    self.post = post 
    if post.username != uid { 
     self.hidden = true 
    } 
} 
+0

您需要通過numberOfRowsInSection –

+0

來管理它。不要隱藏單元格,而是根據僅包含未隱藏帖子的'posts'數組創建另一個數組,並使用該數組來驅動您的tableview。 – Paulw11

回答

3

製作兩個數組並根據您的條件使用,並在任何更新後重新載入您的表。

例:

if(is_searching == true) { 
     cell.textLabel?.text = Array1[indexPath.row]["xyz"] as! NSString as String 
     cell.accessoryView?.hidden = true 

} 
else { 
     cell.textLabel?.text = Array2[indexPath.row]["xyz"] as! NSString as String 
} 

不要忘記重新加載表。

0

您可以使用heightForRowAtIndexPath委託方法,然後返回0對於其對應的數據模型隱藏的單元格爲真。