2016-03-31 2 views
-2

UITableViewCellのタップのCGPointが必要です。これを行うには、サブクラスUITableViewCelltouchesBegan:withEvent:メソッドを使用します。私は、セルに彼らがタップ場所に応じてアクションを行うことができるように、私はその私のUITableViewCellサブクラスからの私の現在のviewControllerに転送するにはどうすればよいtouchesBeganを使用しているUITableViewCellのCGPoint

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    NSLog(@"%@", NSStringFromCGPoint(location)); 
} 

** didSelectRowAtIndexPath:メソッドを呼び出すことができないため、セルにジェスチャ認識機能を使用したくありません。

+0

セルで

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; __weak __typeof(self) weakSelf = self; cell.touchedAtPoint = ^(UITableViewCell *cell, CGPoint point) { [weakSelf doSomethingWithPoint:point); }; return cell; } 

ブロックを使用した例である質問は何ですか?ビューコントローラーをコールバックするセルにデリゲートまたはアクションブロックを追加するだけではできません –

+1

「didSelectRowAtIndexPath:メソッドを呼び出すことができないため、セル上でジェスチャーレコグナイザを使用したくありません」あなたは_already_ですテーブルビューでタッチレスポンスを打ち破る。あなたがすでに持っているコードよりも悪くなることはありません... – matt

+0

あなたがタッチでスーパーに電話して実装を開始したら、すべてが期待どおりに動作するはずです –

答えて

1

委任またはブロックコールバックのどちらでも可能です。以下は

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
{ 
    [super touchesBegan:touches withEvent:event]; 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    void (^touchedAtPoint)(UITableViewCell *, CGPoint) = self.touchedAtPoint; 
    if (touchedAtPoint) { 
    touchedAtPoint(self, location); 
    } 
} 

- (void)prepareForReuse; 
{ 
    [super prepareForReuse]; 

    self.touchedAtPoint = nil; 
} 

IVARの宣言

@property (nonatomic, copy) void (^touchedAtPoint)(UITableViewCell *cell, CGPoint point); 
+0

どのような種類のオブジェクトは 'touchedAtPoint'ですか?私はそれを財産として作る必要があります。 –

+0

私は答えに –

+0

を追加しました。 'void(^ touchedAtPoint)= self.touchedAtPoint;'行に対して '非関数型へのブロックポインタが無効です'エラーが発生しました。 –

関連する問題