[英]Expanding / collapsing selft-sizing table view cell with two labels
我想通過擴展/折疊帶有兩個標簽的自定義大小的單元格來制作表格視圖。 單元格合攏時,我只想顯示標題標簽。 並在單元格展開時顯示標題和詳細信息標簽。 無論折疊還是展開,我都想制作一個自我大小的單元格。
現在,只有在展開時,我才具有自動調整大小的單元格。 但是當我折疊單元格時,我不知道如何設置標題的適當高度。
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CellTableViewCell
tableView.beginUpdates()
if cell.isDetailsHidden {
expandedCells.append(indexPath)
} else {
if let index = expandedCells.indexOf(indexPath) {
expandedCells.removeAtIndex(index)
}
}
tableView.endUpdates()
cell.isDetailsHidden = !cell.isDetailsHidden
return indexPath
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if expandedCells.contains(indexPath) {
return tableView.rowHeight
} else {
return 30.0 // how to find proper height?
}
}
現在,我在tableView(_:cellForRowAtIndexPath)
和tableView(_:willSelectRowAtIndexPath:)
動態選擇適當的單元格類型:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if expandedCells.contains(indexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CellTableViewCell
cell.cellTitle.text = titles[indexPath.row]
cell.cellDetails.text = details[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("cellTitle", forIndexPath: indexPath) as! CellTitleTableViewCell
cell.cellTItle.text = titles[indexPath.row]
return cell
}
}
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if (tableView.cellForRowAtIndexPath(indexPath) as? CellTableViewCell) != nil {
print("with details")
if let index = expandedCells.indexOf(indexPath) {
expandedCells.removeAtIndex(index)
}
} else if (tableView.cellForRowAtIndexPath(indexPath) as? CellTitleTableViewCell) != nil {
print("only title")
expandedCells.append(indexPath)
}
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
return indexPath
}
如果故事板上只有UILabel
,則重新加載會順利進行。 但是,如果我將UIImage
添加到詳細信息單元格,則重新加載會引起一些跳躍,但僅當用戶位於表視圖的底部時才會出現。
在原型單元之間切換是否是一個好的高效解決方案? 如果沒有,我該怎么辦?
預先感謝您的回復。
我不確定圖像跳變問題。 但是最近在我的一個項目中使用了崩潰擴展功能。 而且所有詳細信息都位於單個單元格中,我想它可以使加載過程更加流暢。
我正在為您分享我的代碼段:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(myObject.count == 0 || myObject == nil)
{
let emptyLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
emptyLabel.text = "No Data Found"
emptyLabel.textColor = UIColor.whiteColor()
emptyLabel.font = UIFont.boldSystemFontOfSize(20)
emptyLabel.textAlignment = NSTextAlignment.Center
self.tableView?.backgroundView = emptyLabel
return 0;
}
else
{
self.tableView?.backgroundView = nil
return myObject.count+3
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("SubHeader", forIndexPath: indexPath) as! OrderSubHeaderTableViewCell
cell.totalOrder.format = "%d";
cell.totalOrder.countFrom(0,to: CGFloat(self.myObject.count),withDuration: 1.0)
cell.totalAmount.format = "%.02f"
cell.totalAmount.countFrom(0,to: CGFloat(NSNumberFormatter().numberFromString(self.totalAmount)!),withDuration: 1.0)
cell.more.hidden = detail
return cell
}
if indexPath.row == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("SubHeaderCash", forIndexPath: indexPath) as! OrderSubHeaderSecondTableViewCell
cell.collection.format = "%d";
cell.collection.countFrom(0,to: CGFloat(self.collection),withDuration: 1.0)
cell.cash.format = "%.02f"
cell.cash.countFrom(0,to: CGFloat(NSNumberFormatter().numberFromString(cashAmount)!),withDuration: 1.0)
return cell
}
if indexPath.row == 2 {
let cell = tableView.dequeueReusableCellWithIdentifier("SubHeaderType", forIndexPath: indexPath) as! OrderSubHeaderThirdTableViewCell
cell.delivery.format = "%d";
cell.delivery.countFrom(0,to: CGFloat(self.delivery),withDuration: 1.0)
cell.paypal.format = "%.02f"
cell.paypal.countFrom(0,to: CGFloat(NSNumberFormatter().numberFromString(paypalAmount)!),withDuration: 1.0)
return cell
}
else
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! OrderTableViewCell
cell.setValue(myObject.objectAtIndex(indexPath.row-3) as! Order)
return cell
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2
{
detail = !detail
tableView.setContentOffset(CGPointZero, animated:true)
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
if tableView.cellForRowAtIndexPath(indexPath) != nil {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! OrderSubHeaderTableViewCell
cell.more.hidden = detail
}
tableView.beginUpdates()
tableView.endUpdates()
}
else
{
self.delegate?.orderSelected(self.myObject[indexPath.row-3] as! Order,index: indexPath.row)
if let detailViewController = self.delegate as? OrderDetailsTableViewController {
detailViewController.delegate = self
splitViewController?.showDetailViewController(detailViewController.navigationController!, sender: nil)
}
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return (!detail) ? 100 : 80
}
else if indexPath.row == 1 {
return (!detail) ? 0 : 80
}
else if indexPath.row == 2 {
return (!detail) ? 0 : 100
}
else{
return 130
}
}
這里處於崩潰狀態,只有“ SubHeader”單元格可見,並且在擴展“ SubHeadrCash”和“ SubHeaderType”后可見。 我通過更改單元格高度和使用布爾變量“詳細信息”來控制切換
更新
對於您的方案,您可以通過使用標簽的字符串並調用boundingRectWithSize來計算標簽的動態高度。
let labelWidth = label.frame.width
let maxLabelSize = CGSize(width: labelWidth, height: CGFloat.max)
let actualLabelSize = label.text!.boundingRectWithSize(maxLabelSize, options: [.UsesLineFragmentOrigin], attributes: [NSFontAttributeName: label.font], context: nil)
let labelHeight = actualLabelSize.height
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.