2016-01-21 83 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

相關問題