正如问题所述,我想在UITableViewCell上实现两个不同的动作来轻击和长按。
我估计我必须在每个阶段取消选择行并在此处放置任何函数:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
然后从故事板添加Tap Gestures,但当我将动作拖动到原型单元格时,Storyboard会给我一个错误。提示?
答案 0 :(得分:14)
试试这个 -
在cellForRowAtIndexPath
方法中,您可以单独添加点击手势和长按手势,然后实施它们。这样,您的didselect
功能将不再需要,您不需要deSelect
任何内容。
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
cell.tag = indexPath.row;
[cell addGestureRecognizer:tapGestureRecognizer];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //seconds
[cell addGestureRecognizer:lpgr];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
现在,
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
// Your code here
}
-(void)cellTapped:(UITapGestureRecognizer*)tap
{
// Your code here
}
答案 1 :(得分:0)
您可以使用上面发布的UITableViewDelegate方法处理单击操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
在您的UITableViewCell follow this instructions上执行UILongPressGestureRecognizer。
希望有所帮助。
答案 2 :(得分:0)
<强>夫特强>:
定义这样的手势:
fileprivate func addGestures(inCell cell: UITableViewCell, withIndexPath indexPath: IndexPath) {
let tapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(cellTapped))
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
cell.addGestureRecognizer(tapGestureRecognizer)
let lpgr = UILongPressGestureRecognizer.init(target: self, action: #selector(cellLongPressed))
lpgr.minimumPressDuration = 1.0
cell.addGestureRecognizer(lpgr)
cell.tag = indexPath.row
cell.selectionStyle = .none
}
@objc fileprivate func cellTapped(_ gesture: UITapGestureRecognizer) {
//Do whatever you want to!
}
@objc fileprivate func cellLongPressed(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
//Do whatever you want to!
}
}
并添加这样的手势:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (tableView.dequeueReusableCell(withIdentifier: "CellIdentifier") as! UITableViewCell)
self.addGestures(inCell: cell, withIndexPath: indexPath)
}