如何从UITableViewCell内部加载UITableView



显示某些代码之前的快速解释:

我想创建一个体系结构,其中我有帖子、对这些帖子的回复和对这些回复的回复等(我可以定义一个限制(。

每个回复都是可扩展的。

我不知道我是否采取了正确的方式,但我正在考虑添加一个UITableView作为我的UITableViewCell的子项,以显示回复,并对回复等进行再次操作。

我现在只有一个UITableView,所有的帖子都是来自xib的可重用单元格。由于我无法将UITableView和单元格添加到此xib,我陷入了困境。

这里有一些代码:

发布UITableView函数:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = argumentList.dequeueReusableCell(withIdentifier: "postBox", for: indexPath) as! PostBox
let currentPost = postListItems[indexPath.row]
cell.postBoxView.message = currentPost
return cell
}

PostBox文件:

import UIKit
class PostBox: UITableViewCell {
let apiClient = APIClient.sharedInstance
@IBOutlet weak var postBoxView: PostBoxView!
}

PostBoxView文件:

class PostBoxView: UIView {
var apiClient = APIClient.sharedInstance
var repliesOpen: Bool = false
@IBOutlet var contentView: UIView!
// Bunch of outlets here

var message: Message! {
didSet {
// initializing design stuff here
}
}

override init(frame: CGRect){
super.init(frame: frame)
self.commonInit()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}

func commonInit(){
let bundle = Bundle(for: Self.self)
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
nib.instantiate(withOwner: self, options: nil)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
}

@IBAction func showPostReplies(_ sender: Any) {
self.toggleReplies(show: true)
}

func getReplies() {
apiClient.getReplies(postId: String(self.message.id), sort: "-created_at", perPage: 5, page: 1, completion: { replies in
print(replies)
})
}

func toggleReplies(show: Bool) {
self.repliesOpen = show
switch repliesOpen {
case true:
self.getReplies()
case false:
print("close")
}
}
}

所以我来了,想知道如何在我的帖子中添加回复,然后在回复中添加回复。也许我有点想错了,给我一点帮助就好了。

Mate,在表视图单元格中添加表视图是一个非常糟糕的主意(太离谱了,我不得不打开这个问题(。

就用户界面而言,它是相对直接的。您可以将文章设为节标题,也可以仅设为单元格。无论哪种方式,所有的回复都将是同一个表中的单元格——为它们创建一个不同的UITableViewCell子类&使用缩进来实现层次结构。(UITableviewCell的indentationLevel和indentationWidth属性(展开-折叠(getReplies(的操作不应在单元格中调用,而应通过委托在viewController(或viewModel/presenter,具体取决于您使用的内容(中调用。

您真正关心的应该是为表提供一流的数据结构,该结构将存储帖子+回复+回复扩展状态+回复层次结构级别。

我希望这能让你走上正确的道路。(不要在表视图单元格中添加表视图,如果回复足够深,实际上可能会出现堆栈溢出(

这是我很快想到的一个数据结构(不包括数据字段,但包括运行它所需的所有其他内容。(

protocol Repliable {
var replies: [Repliable] {get}
var replyLevel: Int {get}
var isPost: Bool {get}
}
struct Post: Repliable {
//data fields

let isPost = true
let replyLevel = 0
var replies: [Repliable]
}
struct Reply: Repliable {
//data fields

let isPost = false
var replyLevel: Int
var replies: [Repliable]
}

要使用它,您的表视图应该使用Replable数组。每次展开(获取(回复时,只需将它们添加到数组中的正确位置&向表中添加行。你现在的挑战就是让它们在阵列中保持正确的顺序。喜欢这样->帖子1,回复1,回复一到回复一,回复二,帖子二。。。

最新更新