iOSが開発したcell上のButton識別

3262 ワード

問題のソース:


iOSの開発では、tableViewセルにボタンを置く必要があり、クリックして操作する必要があります.このとき、Buttonがどの行のcellにあるかを認識し、対応するデータを過去に伝える必要があります.この問題を処理する方法は多種多様です.今日、この問題を整理します.本稿以外の方法を提供することを歓迎します.神様は自動的にスキップしてください.本文demo

問題の解決:


以下は主に3つの方法を用いてUItableViewCellにおけるButtonの位置を識別し,それぞれメリットとデメリットがあり,これらの方法を得た友人は自分で選択することができる.
一.tagを使うのが一番一般的な方法で、使い勝手はいいですが、tagを追加するときに他のtagと衝突して不要な面倒をもたらすことができないことに注意しなければなりません.何も言うことはありません.直接コード//cellで-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{TagCell*cell=[tableView dequeueReusableCellWithIdentiifier:@"tagID"forIndexPath:indexPath];cell.titleL.tex=[NSString stringWithFormat:@私は%ld行です"行"、indexPath.row;cell.tagBtn.tag=btnTag+indexPath.row;[cell.tagBtn addTarget:self action:@selector(tapBtn:) forControlEvents:UIControlEventTouchUpInside]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell;}
// 
` - (void)tapBtn:(UIButton *)sender{
NSString *str = [NSString stringWithFormat:@" %ld ",sender.tag-btnTag];
UIAlertController *cc = [UIAlertController alertControllerWithTitle:@" " message:str preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[cc addAction:cancel];
[self presentViewController:cc animated:YES completion:nil];}`

二.次に、addtargetのメソッド属性を使用してeventイベントを取得し、最終的にクリックしたButtonのtableView内の位置を取得し、indexPath、例えば
- (void)tapBtn:(UIButton *)sender event:(UIEvent *)event{
// event  
UITouch *touch = [[event allTouches] anyObject];
// tableView 
CGPoint point = [touch locationInView:tableViewG];
// indexPath
NSIndexPath *index = [tableViewG indexPathForRowAtPoint:point];
if (index) {
    NSString *str = [NSString stringWithFormat:@" %ld ",index.row];
    UIAlertController *cc = [UIAlertController alertControllerWithTitle:@" " message:str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [cc addAction:cancel];
    [self presentViewController:cc animated:YES completion:nil];
}}

この方法の特徴は、いつでも呼び出すことができ、Buttonの位置を取得することです.カスタマイズされたtableViewセルではなく、ButtonをtableViewセルのaccessoryViewとして使用すると、この方法の使用は便利です.
三.エージェントとindexPathを使用してindexPathをcellのプロパティとして渡し、delegateをパラメータとして渡します.これにより、カスタムcellを使用するときに効果的にデカップリングでき、最初の方法のようにtagが多すぎて衝突する問題を回避できます.
cellで:@protocol clickDelegate-(void)clickAtIndex:(NSIndexPath*)indexPath;@end @interface IndexCell : UITableViewCell @property(nonatomic,strong)UIButton *tagBtn; @property(nonatomic,strong)UILabel *titleL; @property(nonatomic,strong)NSIndexPath *myIndex; @property(nonatomic,weak)iddelegate;
@end

VC中:IndexCell *cell = [tableView dequeueReusableCellWithIdentifier:@"indexID" forIndexPath:indexPath]; cell.titleL.text = [NSString stringWithFormat:@" %ld ",indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.delegate = self; cell.myIndex = indexPath;もし皆さんが良い方法があれば、私に伝言を残して交流してください.文章が浅くて、後で更新します.