gpt4 book ai didi

ios - 如何从单元格调用父 TableView 方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:27:56 24 4
gpt4 key购买 nike

我有一个带有标签的自定义单元格,该标签应该触发父/ TableView Controller 的方法。

尽管 XCode 在自动完成建议中为我提供了该方法,但当我点击该特定标签时它会抛出错误:

UITableViewWrapperView showUserProfile unrecognized selector sent to instance

这是我的代码:

    @implementation ItemTableViewCell

@synthesize item;

- (void)awakeFromNib
{
self.authorLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showUserProfile)];
[self.authorLabel addGestureRecognizer:tap];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}

- (void)showUserProfile
{
id parentView = [self superview];
[parentView showUserProfile];
}

@end

谢谢!

最佳答案

这不是将一些 Action 传递给 tableview 的好方法。您必须在自定义类 UITableViewCell 和您的 Controller 之间使用委托(delegate)。

在你自定义的 UITableViewCell.h 中

@protocol myUITableViewCellDelegate

@interface myUITableViewCell : UITableViewCell

@property (nonatomic, assign) id<myUITableViewCellDelegate> delegate;

@end

@protocol myUITableViewCellDelegate <NSObject>
-(void) cellDidTap:(myUITableViewCell*) sender
@end

在你的自定义 UITableViewCell.m 中

...
- (void)showUserProfile
{
[self.delegate cellDidTap:self];
}
...

在你的 Controller 中

-(UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexpath
{
......
cell.delegate = self
......
}

-(void) cellDidTap:(myUITableViewCell*) sender
{
[self showUserProfile];
}

关于ios - 如何从单元格调用父 TableView 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25016564/

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