2016-09-25 16 views
0

UITableViewCellをサブクラス化し、UIViewで初期化しました。 TableViewから、私は親ViewControllerのメソッドを呼び出す必要があります(seguesなどを実行するため)。サブビュー内からUIViewControllerメソッドにアクセスする

ViewControllerを(MVCの原則に関して)サブビューに直接渡すことができないため、私は委譲パターンを作成し、UITableViewにプロトコルを追加し、UIVIewControllerでデリゲートとしてプロトコルを実装しました。

問題は、ViewControllerのサブビューであるUIViewからTableViewがインスタンス化されるため、ViewControllerではなくViewにselfを指定するため、ViewForrollerをdelegateとしてcellForRowAtIndexPathメソッドに割り当てることができないということです。

すべてのテーブルメソッドをViewControllerに移動し、そこからテーブルをインスタンス化すると、すべてがうまく動作します。しかし、それは私が他のViewControllerでコードを再利用することを許しません。

CustomViewController.h

#import "CustomTableViewCell.h" 
@interface CustomViewController : UIViewController<CustomTableViewCellDelegate> 

CustomViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Create a sub-view where the custom table will appear 
    _customUIView = [[CustomUIView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60)]; 

    [self.view addSubview:_customUIView]; 
    [_customUIView populateTheView]; 
} 

CustomUIView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row]; 

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
     cell.delegate = self; // << THIS WILL NOT WORK AS IT WOULD POINT TO THE View, RATHER THAN THE ViewController 
    } 
} 

CustomTableViewCell.h

@protocol CustomTableViewCellDelegate <NSObject> 
    -(void)performSegue; 
@end 
@interface CustomTableViewCell : UITableViewCell 
    @property(weak, nonatomic) id<CustomTableViewCellDelegate>delegate; 
@end 

CustomTableViewCell.m

if ([self.delegate respondsToSelector:@selector(performSegue)]) { 
    [self.delegate performSegue]; 
} 
+0

ビューコントローラがプロパティを実装し、何らかの形で自身への参照を渡すように設定する必要があります。すべてを初期化/作成する方法について、より多くのコードを表示する必要があります – Paulw11

+0

カスタムセルコードを表示します。 –

答えて

0

私は見つけることができる最善の解決策は、通知を使用しています。 viewControllerでは、viewWillAppearメソッドでオブザーバを設定します。またviewWillDisappear方法でそれを削除する必要がありますまたはアプリがクラッシュします:あなたのサブビューの問題で

// In the parent view controller 
- (void)viewWillAppear:(BOOL)animated { 
    // Add observers to perform an action  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageTappedMessageReceived:) name:@"imageTapped" object:nil]; 
} 

-(void) imageTappedMessageReceived:(NSNotification *)notification { 
    NSLog(@"The image was tapped"); 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    NSLog(@"View controller will disappear"); 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"imageTapped" object:nil]; 
} 

あなたは(ボタンを押すか、他のロジック、あなたが持っている)する必要が通知:

// In the subview 
[[NSNotificationCenter defaultCenter] postNotificationName:@"imageTapped" object:self userInfo:tempDictionary]; 

viewControllerの "imageTappedMessageReceived"メソッドは、 "imageTapped"通知がサブビューで呼び出されるたびに発生します。

関連する問題