5

UITableView を含むビューがあります。この UITableView のセルは、さまざまな種類のセルを持つために、Interface Builder で作成されます。そのため、各ボタンのアクションは、次のようなセル クラスで管理されます。

- さまざまな種類のセルを含む私の UITableView :

ここに画像の説明を入力

-タイプ 1セル("CellTypeOne.h")のクラスのヘッダー ファイル:

@interface CellTypeOne : UITableViewCell
{
}

- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;

-タイプ 2セル("CellTypeTwo.h")のクラスのヘッダー ファイル:

@interface CellTypeTwo : UITableViewCell
{
}

- (IBAction)actionForTheUniqueButton:(id)sender;

-私のテーブルビューを含むビュー( "ViewContainingMyTableView.h" ):

@interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    UITableView *myTBV;
}

@property (retain, nonatomic) IBOutlet UITableView *myTBV;

@end

ここで私がやりたいこと:

たとえば、最初のセルの最初のボタンをクリックすると、「現在の」セルの indexPath を表示できるようにしたいと考えています。

たとえば、次の場合に次の出力が必要です。

  • 最初のセルの最初のボタンをクリックします。0
  • 最初のセルの 3 番目のボタンをクリックします。0
  • 2 番目のセルの最初の一意のボタンをクリックします。1
  • 等...
4

5 に答える 5

10

あなたはカスタムセルを使用しているので、セル自体ではなくカスタムセル内のボタンに触れているため、セレクションも処理する必要があると思います。たとえば、カスタムセルで

あなたのCellTypeOne.h追加でこれ

//@class CellTypeOne; //if u want t pass cell to controller
@protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell
 - (void)touchedTheCell:(UIButton *)button;
 //- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `@class CellTypeOne;` at the beginning  
@end

@interface CellTypeOne : UITableViewCell
{

}
@property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate
- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;

あなたのCellTypeOne.mファイルに

@synthesize delegate; //synthesize the delegate 

- (IBAction)actionForFirstButton:(UIButton *)sender 
{ 
   //add this condition to all the actions becz u need to get the index path of tapped cell contains the button
    if([self.delegate respondsToSelector:@selector(touchedTheCell:)])
     {
        [self.delegate touchedTheCell:sender];
        //or u can send the whole cell itself
        //for example for passing the cell itself
        //[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error  in the defining of the delegate add "@class CellTypeOne;" above the defying the delegate
    }
}

そしてあなたのViewContainingMyTableView.h

 @interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table  delegates
 {
    UITableView *myTBV;
 }

 @property (retain, nonatomic) IBOutlet UITableView *myTBV;

 @end

そしてViewContainingMyTableView.mファイルに

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
   //during the creating the custom cell 
   CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
   if(cell1 == nil)
   { 
     cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   }
     cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important
}

//now implement the delegate method , in this method u can get the indexpath of selected cell 

- (void)touchedTheCell:(UIButton *)button
 {
    NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview];
    NSLog(@"%@",indexPath.description);

 }
 /* if u pass the cell itself then the delegate method would be like below
- (void)touchedTheCell:(CellTypeOne *)myCell
 {
    NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path
    //now by using the tag or properties, whatever u can access the contents of the cell
    UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button 
    //... u can access all the contents in cell 
 }
 */

あなたの場合、これはセル内の最初のボタン用であり、異なる機能を持つ各ボタンにデリゲートメソッドを追加し、セル内の別のボタンに対して上記の手順を繰り返します

あなたがこれを手に入れることを願っています:)ハッピーコーディング

于 2013-10-29T12:25:49.487 に答える