2016-01-21 80 views
0

我有一个UITableView填充了其上有按钮的单元。我希望这些按钮在编辑模式下也可以工作,但他们不会。看起来像UITableViewCell上的手势识别器正在阻止按钮上的手势识别器。有没有人有任何关于解决这个问题的建议?在编辑模式下UITableViewCell上的按钮无法工作

ViewController.m:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.items = [@[@"one", @"two", @"three", @"four"] mutableCopy]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cellID" 
                  forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[TableViewCell alloc] init]; 
    } 

    [cell.button setTitle: self.items[indexPath.row] 
       forState: UIControlStateNormal]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.items removeObjectAtIndex: indexPath.row]; 
    } 
} 

TableViewCell.h:

@interface TableViewCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UIButton *button; 
- (IBAction)buttonTapped:(id)sender; 
@end 

因此,buttonTapped:被称为当一个小区不在编辑模式,但是当细胞处于编辑模式,该按钮不起作用。

+0

你可能想告诉你使用,1键是如何添加的代码,2-如何启用“编辑”模式,3-您使用的任何手势识别器。 – zcui93

+0

我指的是gestureRecognizers的UIKit中的使用。按钮在故事板 – poisoned

回答

-1

要做到这一点,你需要继承的UITableView并使其符合一个UIGestureRecognizerDelegate协议。在你的UITableView子类的.m文件添加以下代码:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
     shouldReceiveTouch:(UITouch *)touch { 
    if ([touch.view isKindOfClass: [UIButton class]]) { 
     return NO; 
    } 
    return YES; 
} 
0

使用单元格中每个按钮的不同标签。例如,

Inside cellforRowAtIndexPath,指定一个按钮的标记。

[Button addTarget:self action:@selector(func:event:) forControlEvents:UIControlEventTouchUpInside]; 
[Button setTag:indexPath.row]; 

现在调用函数并使用标记引用单元格。

-(IBAction)func:(id)sender event:(id)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint Pos = [touch locationInView:self.tableview]; 
    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:Pos]; 
    if(indexPath != nil){ 
     //do operation with indexPath 
    } 
} 

希望它可以帮助...

0

首先看到一些代码,你做了什么!没有适当的声明就很难给出答案。 首先我的建议是在UItableview委托方法中给手势识别。

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

// code 

} 

有时候手势在编码方面的小错误没有用。其次如果可能的话,

而是直接加入手势识别到Cell的

,您可以将其添加到viewDidLoad中的UITableView的。 这是在UITableview中操作手势的最佳方式。

正如Phix例中,

说在didSwipe法,您可以确定受影响IndexPath和细胞如下:

-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { 

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; 
     NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
     UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; 
     // ... 
    } 
} 

您可以设置其他手势等形式设置。 我希望它能解决你的问题。

+0

对不起加入,我的问题没有得到很好的制定。我更新了它,所以如果你现在看到任何解决方案的检查。无论如何感谢您的帮助 – poisoned

相关问题