I have UITableView
它使用自定义UITableViewCell
含有一个UITextField
.
我想调用一个方法(myMethod
)当点击(UIControlEventTouchDown
)并尝试将其连接到内部UITableView
委托方法cellForRowAtIndexPath
通过执行以下操作:
[tf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
当。。。的时候UITextField
单击后,什么也没有发生。
我试图为另一个人做同样的事情UITextField
之外的UITableView
:
[othertf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
当我点击othertf
该方法按照我的预期被调用。
我有点困惑,因为除了我交换的代码之外,代码是相同的tf
for othertf
.
下面是完整的代码cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = @"DetailFieldView";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:DetailCellIdentifier owner:self options:nil];
cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITextField *tf = (UITextField *)[cell viewWithTag:2];
tf.text = @"some value";
[othertf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[tf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
return cell;
}
谁能发现我做错了什么吗?这可能很简单,因为我是 iOS 开发新手。