2016-01-05 53 views
0

我跟着這個程序建立一個表定製單元:https://github.com/awseeley/Custom-Table-Cell隱藏元素 - 斯威夫特

單擊單元格時,我試圖擴大細胞,並顯示不同的內容。

以下是我在我的視圖控制器:

var cellTapped:Bool = true 
var currentRow = 0; 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //CODE TO BE RUN ON CELL TOUCH 

    let selectedRowIndex = indexPath 
    currentRow = selectedRowIndex.row 

    let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TblCell 

    cell.image.hidden = true 

    tableView.beginUpdates() 
    tableView.endUpdates() 

} 



func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if indexPath.row == currentRow { 
     if cellTapped == false { 
      cellTapped = true 

      return 141 
     } else { 
      cellTapped = false 
      return 70 
     } 
    } 
    return 70 
} 

單擊時的電池膨脹並再次點擊時收縮。但圖像並不隱藏。我如何讓圖像隱藏?當單元格被選中並且具有展開/摺疊效果時,是否有更好的方式顯示另一個自定義.xib文件?

回答

1
  1. 您應該添加下面這個工具你cellForRowAtIndexPath

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    
        if indexPath.row == currentRow { 
         if cellTapped == false { 
          cell.image.hidden = false 
         } else { 
          cell.image.hidden = true 
         } 
        } 
    
        return cell 
    } 
    

而且didSelectRowAtIndexPath你應該刪除錯誤的代碼:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //CODE TO BE RUN ON CELL TOUCH 

    let selectedRowIndex = indexPath 
    currentRow = selectedRowIndex.row 
    tableView.reloadData() 

} 
  • 如果你想使用想用tableView.beginUpdate()爲更好的動畫,你可以參考我的演示:

    Demo hide image on cell

  • 希望這有助於!

    +0

    謝謝,但這只是從單元格中永久移除圖像,我如何才能在單元格被選中時移除圖像,並在不再選擇單元格時顯示圖像? –

    +1

    @KatieH你想在'tableView'上只隱藏一個單元格,或者每個單元格點擊時都會顯示/隱藏? –

    +0

    我只想在單元格處於活動狀態時單擊/選擇它時隱藏圖像。當單元格不再被選中時,圖像應該再次出現。 –