2015-09-22 20 views
2

我想有一個UITableViewCell揭示特定的細胞更多的內容每當特定的細胞被竊聽。一旦細胞擴大並再次敲擊,細胞應縮回原始大小。如何更改的UITableViewCell的自來水高度?

我敢肯定,代表heightForRowAtIndexPathdidSelectRowAtIndexPath需要做些事情,但我不知道如何使用didSelectRowAtIndexPath選擇特定的表格單元格行。

// Height of table cell rows 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 45 
} 

//On cell tap, expand 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.tableView.rowHeight = 75; 
} 

此外,是否有可能隱藏任何內容是從父細胞溢出?有點像溢出:隱藏;在CSS中。

回答

12

聲明一個全局變量NSInteger的類型和存儲在didSelectRowAtIndexPath方法選擇的tableview 並重新加載在這裏。經過heightForRowAtIndexPath和提升高度有檢查行。 嘗試像這樣

var selectedIndex : NSInteger! = -1 //Delecre this global 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.row == selectedIndex{ 
     selectedIndex = -1 
    }else{ 
     selectedIndex = indexPath.row 
    } 
    tableView.reloadData() 
} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if indexPath.row == selectedIndex 
    { 
     return 75 
    }else{ 
     return 45 
    } 
} 
+0

注意你沒有得到任何的動畫與此解決方案,不像法伊扎的回答 – Joey

4

你所要做的就是保存didSelectRow方法選定單元格的索引。並且還必須在桌面視圖上開始/結束更新。這將重新加載tableview的一部分。並會調用heightForRow方法。在這種方法中,你可以檢查是否選擇你行之一,然後返回expandedHeight,否則返回正常高度

在高度爲行:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

    if self.selectedSortingRow == indexPath.row { 
     return ExpandedRowHeight 
    } 
    else{ 
     return normalHeight 
    } 
} 

在didSelect行:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    self.tableView.beginUpdates() 
    selectedSortingRow = (int) indexPath.row   
    self.tableView.endUpdates() 
} 
+0

讓我知道,如果你沒有得到這樣的 –

+0

明白了,這和對方的回答幫一噸,謝謝! – 10000RubyPools

+0

請注意,您不必保存自己的變量跟蹤此爲表視圖通過'tableView.indexPathForSelectedRow'跟蹤其選擇指數路徑 – Joey

相關問題