gpt4 book ai didi

ios - 访问 subview 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 12:59:52 25 4
gpt4 key购买 nike

接下来是我的 View 层次结构:

  1. 我的 Root View Controller 包含自定义 View
  2. 包含自定义 UITableView 的自定义 UIView
  3. 我的自定义 UITableView

我想使用自定义 UITableView 的委托(delegate)方法,但我真的不知道如何访问它的委托(delegate),因为 UITableView 已添加到我的自定义 UIView 类中。 请澄清一下,我对这个问题有点困惑。

这是我的代码:

将我的自定义 UITableView 添加到 UIView(我正在使用 CollapseClick ):

-(void) initMehod{
DetailsView *testView = [[[NSBundle mainBundle] loadNibNamed:@"DetailsView" owner:self options:nil] lastObject];

test1View = testView;

//collapse click init
CGRect viewRect = [test1View frame];
viewRect.origin.x = 10;
test1View.frame = viewRect;

[test1View setBackgroundColor:[UIColor magentaColor]];

self.myCollapseClick.CollapseClickDelegate = self;
[self.myCollapseClick reloadCollapseClick];

// If you want a cell open on load, run this method:
[self.myCollapseClick openCollapseClickCellAtIndex:0 animated:NO];
}
...

我正在使用 PagedFlowView在我的 Root View Controller 中添加自定义 View :

- (UIView *)flowView:(PagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{

mainDetailV = (MainDetailView *)[flowView dequeueReusableCell];

if (!mainDetailV) {
mainDetailV = [[[NSBundle mainBundle] loadNibNamed:@"MainDetailView" owner:self options:nil] lastObject];
//mainDetailV.layer.cornerRadius = 6;
mainDetailV.layer.masksToBounds = YES;
}

[usedViewControllers insertObject:mainDetailV atIndex:index];

return mainDetailV;
}

一切正常,除了我不能使用我的自定义 UITableView 委托(delegate)(例如 didSelectRowAtIndexPath: 方法)。

编辑:我已经解决了我的问题,PagedFlowView 实际上处理标记手势识别器并覆盖我的自定义 tableView 委托(delegate)方法。

类似问题:

最佳答案

首先,您的可折叠 tableview 抽象了 tableview 委托(delegate)(即它的包装器)。它为您提供了 didClickCollapseClickCellAtIndex 方法。用那个。

看来您对委托(delegate)模式的理解有问题。避免破坏委托(delegate)模式的级联效应。它使调试变得容易并有助于理解代码的流程。下图可能会帮助您理解我的意思

Delegation pattern

在自定义 View 而不是 rootViewcontroller 中实现您的 didSelectRowAtIndexPath。

// This is your custom view-(id)init{     ----    self.myCollapseClick.CollapseClickDelegate = self;     ----}//collapsable delegate-(void)didClickCollapseClickCellAtIndex:(int)index isNowOpen:(BOOL)open {  // you need to create your own custom view delegate with mehthod didSelectIndex:index   [self.customViewDelegate didSelectIndex:index];}

在你的 rootviewcontroller 中你需要这样做

customView.customViewDelegate = self;

-(void)didSelectIndex:(int)index{   // do your stuff here}

关于ios - 访问 subview 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060943/

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