我正在尝试让我的单元格的某个区域响应点击手势。为此,我在单元格中添加了一个名为“touchViewProfile”的 UIView。此代码已按原样添加到 cellForRowAtIndexPath 中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
}
// Tag Gesture Recognizer for Profile
UITapGestureRecognizer *tapProfile = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(showProfile)];
tapProfile.numberOfTapsRequired = 1;
tapProfile.numberOfTouchesRequired = 1;
cell.touchViewProfile.userInteractionEnabled = YES;
[cell.touchViewProfile addGestureRecognizer:tapProfile];
return cell;
}
它适用于除第一个单元格之外的每个单元格。我忘了什么吗?我在 .h 中声明了“UIGestureRecognizerDelegate”
编辑:“touchViewProfile”是我在 SimpleTableCell.xib 中使用 Interface Builder 创建的 UIView。这是一个属性,在 .m 中合成
最佳答案
您首先需要有单元格索引值,因此我们需要实现 UI Gesture Delegate
-(void)showProfile:(UIGestureRecognization *)recognize{
if(recognize.state==UIGestureRecognizeStateEnded){
CGPoint touched=[recognize locationInVie:self.tableView];
NSIndexPath *touchIndex=[self.tableView indexPathForRowAtPoin:touched];
SimpleTableViewCell *touchedCell=[self.tableView cellForRowAtIndexPath:touchedIndex];
//your code
}
}
关于ios - UITableView 中的 UITapGestureRecognizer - 第一个单元格被点击手势忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356138/