21

UICollectionViewCellアクションが呼び出されたときのアニメーションを作成したい。
私もやってUICollectionViewCellましInterface BuilderUICollectionViewindexPath今、私は自分のactionBtnAddToCard方法で正しいものを得たいと思っています。

それが私が今試している方法です(ProduktViewCell.mの方法):

- (IBAction)actionAddToCart:(id)sender {
    XLog(@"");

    // see this line
    NSIndexPath *indexPath = ??** how can i access the correct indexPath**??;
    SortimentViewController *svc = [[SortimentViewController alloc] initWithNibName:@"SortimentViewController_iPad" bundle:[NSBundle mainBundle]];
    [svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];

    [svc collectionView:svc.collectionViewProdukte didSelectItemAtIndexPath:indexPath];
} 

SortimentViewController は、UICollectionView を継承する viewController です。
正しいindexPathにアクセスするには?

更新 1: 理解を深めるために投稿を編集しました。

4

13 に答える 13

16

ビュー階層がわかっている場合は簡単です。

UIButton *button = (UiButton *) sender;

ボタンが次のような場合 ->UITableViewCell->ボタン

その後、このようなセルを取得できます

UITableViewCell *cell = (UITableViewCell *)[button superview];

ボタンが次のような場合 ->UITableViewCell->コンテンツビュー->ボタン

UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

そして最後に、このようにインデックスパスを抽出できます

NSIndexPath *indexPath = [self.table_View indexPathForCell:cell];
于 2012-12-14T09:40:11.903 に答える
7

ビューに依存しないでください。これを試して。

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:buttonPosition];

NSLog(@"%ld", (long)indexPath.row);
于 2015-02-23T13:59:21.233 に答える
2

特定のセルをアニメーション化する場合は、そのセルへの参照を取得する必要があります。単に呼び出す

[svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];

何もしません。次のように、メソッドが返すセルを保持する必要があります。

UICollectionViewCell *cell = [svc.collectionViewProdukte cellForItemAtIndexPath:indexPath];

その後、先に進んでアニメーション化します。

[UIView animateWithDuration:0.2f animations:^{
    cell.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
}];
于 2012-12-14T09:10:32.430 に答える
1
//Note: this is for a storyboard implementation

// here is code for finding the row and section of a textfield being edited in a uicollectionview
UIView *contentView = (UIView *)[textField superview];
UICollectionViewCell *cell = (UICollectionViewCell *)[contentView superview];
cell = (UICollectionViewCell *)[contentView superview];

// determine indexpath for a specific cell in a uicollectionview
NSIndexPath *editPath = [myCollectionView indexPathForCell:cell];
int rowIndex = editPath.row;
int secIndex = editPath.section;
于 2014-07-31T18:40:52.340 に答える
0

ほとんどの場合、UICollectionViewCell サブクラスがあります。プロパティを追加して、cellForItemAtIndexPath に indexPath を設定するだけです。

于 2015-12-07T06:13:53.960 に答える
0

Xcode10. スウィフト 4.2 バージョン。

extension UICollectionView {

  func indexPathForView(view: AnyObject) -> IndexPath? {
      guard let view = view as? UIView else { return nil }
      let senderIndexPath = self.convert(CGPoint.zero, from: view)
      return self.indexPathForItem(at: senderIndexPath)
  }

}

使用法:

// yourView can be button for example
let indexPath = collectionView.indexPathForView(view: yourView) 
于 2018-10-01T13:45:02.997 に答える
0
 internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “cell_id”, for: indexPath)
    
    let bttn_obj = UIButton(frame: CGRect(x: 5.5, y: 5.5, width: 22, height: 22))
    bttn_obj.addTarget(self, action: #selector(bttn_action), for: UIControl.Event.touchUpInside)
    cell.addSubview(bttn_obj)        
    
    return cell
}

@IBAction func bttn_action(_ sender: UIButton) -> Void {
    
    let cell_view = sender.superview as! UICollectionViewCell
    let index_p : IndexPath = self.collectionview.indexPath(for: cell_view)!
    
    print(index_p)
 
}
于 2021-06-24T17:40:45.880 に答える