gpt4 book ai didi

ios - 在调用 ParentViewController 的委托(delegate)方法之前将消息从 ChildViewController 传递到 ParentViewController

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:33 25 4
gpt4 key购买 nike

场景:我有 2 个 VC -

subview Controller 它有一个显示项目列表的 tableView。在表填充到我的 ParentVC 之后,我需要传递 tableView.contentSize.height 值。为此,我使用委托(delegate)作为

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableVieww.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
cell.textLabel?.text = "heyy"

hght.constant = tableVieww.contentSize.height
if flag == true
{
delegate.tableHeight(tableVieww.contentSize.height)
print(tableVieww.contentSize.height)
flag = false
}
return cell
}

父 View Controller

它有一个带有一个单元格的 tableView。此单元显示 childVC 的 View ,即 nwVC。我想根据我的 ChildVC 的 tableView 的高度更改单元格高度。

我正在通过以下代码添加 childVC 的 View ,我知道这是错误的地方,但我不知道如何、在哪里以及做什么,才能在 ParentViewController 的函数之前调用 childViewController 的函数?

vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("nwVC") as? nwVC//newVC is ChildViewController
vc3!.view!.frame = cell.myview.bounds
vc3!.didMoveToParentViewController(self)
cell.myview.addSubview(vc3!.view)//UIView inside the cell
vc3!.delegate=self

问题 -

ParentViewController 的 tableView 的委托(delegate)方法在调用 childViewController 的函数之前被调用,为此我无法根据 childVC 的表内容更新我的 rowHeight。

最佳答案

最后,我想出了一些可行的方法,但我仍然希望 iOS 开发人员在查看此问题时提出建议。

马克: 我无法在加载 ParentVC 的 TableView 委托(delegate)函数之前执行加载 childVC 的函数,但我做了一些工作得很好的东西。

在我的 ParentVC 中

override func viewWillAppear(animated: Bool) {
vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("nwVC") as? nwVC
addChildViewController(vc3!)
vc3!.didMoveToParentViewController(self)
vc3!.delegate=self
}

//childVC's delegate function implementation
func tableHeight(height: CGFloat) {
height = height//I get the table view height from the childVC,height is a variable declared as var height = 200.0(it can be any value > 0)
print(ht)
self.tableVIeww.reloadData()//reload my tableView
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return hgt//at first call it returns the default value but in the 2nd call it returns the value sent by childVC
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableVIeww.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath) as! myTVC
cell.backgroundColor = UIColor.greenColor()
vc3?.view.frame = cell.myview.bounds
cell.myview.addSubview((vc3?.view)!)//myView is the view in the cell's content view pinned to its edges.

return cell
}

优缺点

专业人士

最大的优势是您可以完成工作。

缺点

如您所见,ChildVC 的 View 被添加了 2 次(1 次使用高度变量的默认单元格大小,第二次是在表格重新加载时)。我觉得这可能会稍微影响性能,如果数据是动态的,它可能会处理一段时间。

请随时提出建议...

关于ios - 在调用 ParentViewController 的委托(delegate)方法之前将消息从 ChildViewController 传递到 ParentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629538/

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