我正在创建一个新的UIViewController(称之为MyViewController),并将视图(MyView)添加为TableViewCell的子视图。在MyView中,有一个按钮。该按钮是在MyViewController的init期间以编程方式创建的,如下所示:
-(id)initWithFrame:(CGRect)frame {
self = [super init];
if(self) {
[self.view setFrame:frame];
_yesButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-150, 40, 140, 30)];
[_yesButton setTitle:@"Yeah!" forState:UIControlStateNormal];
[_yesButton addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_yesButton];
}
return self;
}
现在,似乎很简单。它显示正常,模拟器中的一切看起来都很棒。
但是当我点击MyView中的“_yesButton”时,我遇到了这个错误:
-[_UITableViewCellSeparatorView didClick]: unrecognized selector sent to instance 0x7b7f7ec0
什么?什么时候“_UITableViewCellSeparatorView”进入等式?我特意告诉_yesButton将Target设置为“self”,所以选择器应该发送到MyViewController,对吧?我甚至可以想象它被绊倒并将其发送到UITableViewCell,因为MyView嵌入在TableViewCell中,但为什么是SeperatorView?
有人能告诉我如何让我的_yesButton将回调发送回MyViewController吗?对于奖励积分,你能解释一下“_UITableViewCellSeparatorView”在这个对话中是怎么回事吗?
编辑:以下是我在TableView中构建单元格以及向其添加MyView的方法。请注意,我故意不使用此行的队列,但如果它是问题的根源,则可能会改变。
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyViewCell"];
MyViewController *myViewController = [[MyViewController alloc] initWithFrame:cell.contentView.frame];
[cell.contentView addSubview:myViewController.view];
return cell;
而且didClick方法目前是空的,(它从来没有到达那里,所以我在编写时没有那么远),但它目前在MyViewController中定义为:
-(void)didClick {
}
答案 0 :(得分:3)
解决方案#1
实际上这是因为ARC没有保留MyViewController
。正在调用dealloc
方法ID。在UITableViewController
中添加控制器的ans实例将解决问题并使ARC保留您的控制器。
解决方案#2
尝试这样的事情。
创建自定义UITableViewCell
:
MyCustomCell.h:
#import <UIKit/UIKit.h>
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) UIButton *yesButton;
-(id)initWithFrame:(CGRect)frame;
@end
MyCustomCell.m:
#import "MyCustomCell.h"
@implementation MyCustomCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(id)initWithFrame:(CGRect)frame {
self = [super init];
if(self) {
[self.view setFrame:frame];
_yesButton = [[UIButton alloc] initWithFrame:CGRectMake(frame)];
[_yesButton setTitle:@"Yeah!" forState:UIControlStateNormal];
[self.contentView addSubview:_yesButton];
}
return self;
}
@end
在UITableViewController
函数的viewDidLoad
中:
[self.tableView registerClass:[MyCustomCell class] forCellReuseIdentifier:CellIdentifier];
然后在tableView:cellForRowAtIndexPath:
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierr forIndexPath:indexPath];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithFrame:frame];
[cell.yesButton addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
}
现在在UITableViewController
中实施以下内容:
-(void)didClick {
// The following identify the in which cell the action has been triggered
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
// Do whatever you want for the given cell
}
}
答案 1 :(得分:0)
我认为你采用了错误的做法。
视图单元格不像视图一样存在于内存中。我们可以说,一旦绘图完成,它就不再存在了。
对于所有单元格操作,您应该将TableViewController设置为target,并处理此控制器中的click方法。 只有该控制器具有单元格和单元格格式数据的全局知识。
您可以将按钮的标记设置为行索引,以了解已单击的行。
你的didClick选择器也不正确。它应该有这种形式
-(void)didClick:(id)sender
{
NSInteger clickedRow = ((UIButton*)sender).tag
}
所以这样连接:
[_yesButton addTarget:<YourTableController> action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
答案 2 :(得分:-1)
问题是,你想要的事件是MyViewController
,但事实并非如此。它取而代之的是UITableViewCellSeparatorView
。
最大的错误是您尝试通过类UIViewController
不支持的方法初始化视图控制器,而是UIView
的一部分:
- (id)initWithFrame:(CGRect)frame
我建议你在继续之前先复习一下。