【发布时间】:2017-08-24 03:38:29
【问题描述】:
我有一个UITableViewController 和一个自定义UITableViewCells 列表。单元格有文本,有时很长。我在表格的 2 行截断了它。
当用户触摸单元格时,我想创建一个UIView 作为弹出窗口,其信息与UITableViewCell 完全相同,并在UITableViewCell 内容上方添加一个标题。所以,本质上,我想要类似以下的东西:
我已经构建了VersePopupView,如图所示,其中文本“自定义标题”实际上是一个UILabel。我已经从 XIB 文件实例化了视图并成功设置了 UILabel 文本,但自定义 UITableViewCell 没有显示,即使我也分配了该值。我的IBOutlets 都与 IB 挂钩。
这是我的实例化:
let popupView: VersePopupView = VersePopupView.instanceFromNib()
popupView.frame = CGRect(x: 10, y: 100, width: 300, height: 100)
popupView.assignVerseTableViewCell(cell: cell)
window.addSubview(popupView)
window.makeKeyAndVisible()
这是我的 VersePopupView 类:
class VersePopupView: UIView {
@IBOutlet weak var cell: VerseTableViewCell!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var headerView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
class func instanceFromNib() -> VersePopupView {
return UINib(nibName: "VersePopupView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VersePopupView
}
func assignVerseTableViewCell(cell: VerseTableViewCell)
{
// Assign it
self.cell = cell
// Get the verse details
if let verseDetails = cell.verse_details {
self.title.text = verseDetails.verseReference()
// Adjust the frame
// Adjust the text to be attributed text
// Set the title to the verse reference
// Anything else?
}
}
}
我做错了什么导致 UITableViewCell 没有出现?
有没有比嵌入 UITableViewCell 更好的模式?
我不想重复代码(我过去做过),因为我希望用户与弹出窗口进行交互,就像他们在表格单元格中一样。
【问题讨论】:
-
不要尝试在表格视图之外使用表格视图单元格...
-
@matt - 为什么不呢?那我应该使用什么模式呢?如何避免重复所有 UI 元素以及与这些元素的交互?我可以将
UITableViewCell本身显示为弹出窗口就好了。如果我不需要额外的标题,我会很好。但我也需要标题。 -
不确定您对“交互”的含义。这不是视图控制器的视图,因此您无法连接到它之外的任何东西。无论如何,如果是我,我会有一个视图 xib 并将其视图放入单元格的内容视图和另一个视图中。
-
或者甚至将视图显示在单元格 xib 中,然后根据需要将其拉出。
-
你在想它。创建一个外观符合您要求的 UIView 子类,然后将其嵌入表格单元格中并在弹出窗口中使用它。
标签: ios swift uitableview uiview