2015-05-04 136 views
0

我試圖在選擇自定義單元格時調整標籤的高度(以允許其展開以使更多文本可見)。更改didSelectRowAtIndexPath中的自定義單元格的標籤高度

我很明顯缺少一些基本的東西,雖然因爲在我嘗試繪製新的CGRect後框架完全相同。

下面是相關代碼:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let indexPath = tableView.indexPathForSelectedRow() 
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! BasicCell 
    UIView.animateWithDuration(0.3) { 
     tableView.beginUpdates() 
     currentCell.subtitleLabel!.frame = CGRectMake(currentCell.subtitleLabel!.frame.origin.x, currentCell.subtitleLabel!.frame.origin.y, currentCell.subtitleLabel!.frame.size.width, 100) 
     currentCell.subtitleLabel!.numberOfLines = 0 
     tableView.endUpdates() 
    } 
} 

任何幫助,將不勝感激。

+0

嘗試添加一個布爾到您的自定義單元格,並在heightForRowAtIndexPath方法設置的高度與細胞contentSize.height否則tableView.rowHeight –

回答

1

您應該使用

-reloadRowsAtIndexPaths:withRowAnimation: 

beginUpdateendUpdate對。僅僅設置框架對於表格視圖來說不足以知道哪個單元需要更新。

此外,不需要將更新代碼放入動畫塊中。它天生就是動畫。

+0

所以順便我試過'tableView.reloadRowsAtIndexPaths([indexPath!],withRowAnimation:UITableViewRowAnimation.None )'之前,並認爲它沒有工作,因爲標籤似乎並沒有更新。但是回去並檢查這些幀,它確實改變了這個幀。現在我必須弄清楚標籤沒有更新的情況。感謝你的回答。 – allocate

相關問題