我有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了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 开发的新手。
