我想通过扩展/折叠具有两个标签的自定义单元格来制作表格视图。我想在单元格折叠时仅显示标题标签。并在展开单元格时显示标题和详细信息标签。无论是折叠还是扩展,我都想制作自己大小的单元格。
现在我只有在扩展时才有自定义单元格。但是当我崩溃细胞时,我不知道如何设置合适的标题高度。
With Worksheets("Inventory Management")
Dim FindFrame As Range
Set FindFrame = .Range("B6:B41").Find(frame)
If Not FindFrame Is Nothing Then
.Cells(FindFrame.Row, 6) = Price
.Cells(FindFrame.Row, 20) = .Cells(FindFrame.Row, 20) + purchq
End If
End With
现在,我在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
添加到详细信息单元格,则重新加载会导致跳转,但只有当用户位于表格视图的底部时才会显示。
在原型电池之间切换是一种好的有效解决方案吗?如果没有,我该怎么做呢?
提前感谢您的回复。
答案 0 :(得分:0)
我不确定图像跳跃问题。但最近在我的一个项目中使用了崩溃扩展功能。并且所有细节信息都在单个单元格中,这使得它可以顺利加载。
我正在为您分享我的代码段:
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”可见。我通过改变Cell高度并使用布尔变量“detail”来控制切换
<强>更新强>
对于您的场景,您可以使用其字符串并调用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