简体   繁体   English

iOS-UITableViewCell中的UIButton的长按手势识别器

[英]iOS - Long Press Gesture Recognizer for UIButton in UITableViewCell

I have a UITableViewCell with 3 UIButton s in it used to decrement a counter. 我有一个带有3个UIButtonUITableViewCell ,用于递减计数器。 On long press one of the buttons, I would like to set the counter to 0. 长按其中一个按钮,我想将计数器设置为0。

Doing this in Interface Builder, I dragged a Long Press Gesture Recognizer onto my button and connected the selector to an IBAction specified in my UITableViewCell.m . 在Interface Builder中执行此操作,将Long Press Gesture Recognizer拖到我的button ,并将选择器连接到UITableViewCell.m指定的IBAction

That's all I did but when I run the app it gives the following error. 这就是我所做的一切,但是当我运行该应用程序时,出现以下错误。

'NSInternalInconsistencyException', 
reason: 'invalid nib registered for identifier (editQuotaCell) 
- nib must contain exactly one top level object which must be a UITableViewCell instance'

Am I missing any steps? 我是否缺少任何步骤?

Just create the button in coding wise add the gesture and selector. 只需在编码中创建按钮即可,明智地添加手势和选择器。 It will work. 它会工作。 Check the bellow code 检查下面的代码

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
     if(cell == nil)
     {
         UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
         cell.backgroundColor = [UIColor clearColor];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;

         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
         [btn addTarget:self action:@selector(select_Action:) forControlEvents:UIControlEventTouchUpInside];
         [btn setTag:Selection_Tag];
         [btn setBackgroundImage:[UIImage imageNamed:@"Demo03.png"] forState:UIControlStateNormal];
         [btn setFrame:CGRectMake(0,0,tableView.frame.size.width,tableView.rowHeight)];
         [cell.contentView btn];

        //Add Your gestures here
     }


     return cell;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM