gpt4 book ai didi

ios - 使用 UIViewController 作为 TableView 单元格

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:14 25 4
gpt4 key购买 nike

我有一个分段控件,它在两个 UIViewController 之间切换。一个信息 View 和一个 ListView (TableView)。

我想使用第一个 UIViewController 作为我的 TableView 的第一个单元格(在另一个部分)。

有没有办法将 UIViewController 转换为单元格或以某种方式将其用作 TableView 的单元格?

最佳答案

以您自己的方式使用此代码。在这里,我们将 Controller View 添加为单元格的 subview ,并使用自动布局来正确管理。您只需要理解并使用代码即可。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath)
cell.layoutIfNeeded()

let infoVC = self.storyboard.instantiateViewControllerWithIdentifier("InfoVC")
self.addChildViewController(infoVC)
cell.contentView.addSubview(infoVC.view)

infoVC.view.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
cell.contentView.addConstraint(NSLayoutConstraint(item: infoVC.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))

infoVC.didMoveToParentViewController(self)
infoVC.view.layoutIfNeeded()
}

关于ios - 使用 UIViewController 作为 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com