0

カスタムセルにテーブルビューを持つView Controllerがあります。

カスタムセルにボタンがあります

MyViewController
---View
------TableView
-------------Custom cell
-------------------UIButton

カスタムセルクラスのカスタムセルにそのボタンのボタンアクションを実装したいと思います。

ボタンをクリックして、mailPageという別のviewcontolerを表示したい

-(IBAction)webButtonClicked:(id)sender
{
  [self presentModalViewController:mailpage animated:YES];
}

しかし、ここでself は CustomCell を意味します。superview で試しても、self の代わりにビュー コントローラーを表示できませんでした。

私はこのように試しましたが、役に立ちませんでした。

MyViewController *myViewController =self.superview 

現在のカスタム セルを含むビュー コントローラーを取得する方法

4

3 に答える 3

4

ビュー コントローラーのプレゼンテーション ロジックは、. ではなくビュー コントローラーに配置することを強くお勧めしますUITableViewCell

既にカスタム セルを使用しているため、これはかなり簡単です。カスタム セルに新しいプロトコルを定義し、View Controller をデリゲートとして機能させるだけです。または、この回答 hereに従って、デリゲートを完全に忘れて、ビューコントローラーをボタンのターゲットとして機能させることもできます。

カスタムUITableViewCellには、それが表示されているView Controllerの依存関係や知識がまったくないはずです。

于 2013-07-08T11:54:26.843 に答える
2

簡単な方法は、セル内のボタンに一意のタグを設定することです.cellForRowAtIndexpathメソッドでは、ボタンインスタンスを次のように取得できます

UIButton *sampleButton=(UIButton *)[cell viewWithTag:3]; 

アクションを次のように設定します

    [sampleButton addTarget:self action:@selector(sampleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

ビューコントローラーでアクションを設定します

-(void)sampleButtonPressed:(id)sender
{
}
于 2013-07-08T11:54:46.400 に答える
2

これを試して:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"OpenHouseListCustomCell";
    OpenHouseListCustomCell *cell = (OpenHouseListCustomCell *)[tblOpenHouses dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"OpenHouseListCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.showsReorderControl = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];
        [cell.btn1 addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }

    cell.btn1.tag = indexpath.row;
    return cell;
}

-(void) ButtonClicked {
    //your code here...
}
于 2013-07-08T11:58:49.653 に答える