2017-03-02 3 views
2

カスタムセルのボタンで警告を表示したい。 Objective Cでこれを行うにはどうすればよいですか? おかげUITableViewCellクラスからの警告を表示 - Objective c

+0

Googleでこれを試しましたか? "objective-c button tableview"は、役に立つかもしれない多くの結果を提供します。 – Ralfonso

+0

私はボタンを追加する方法を知っていますが、私は警告を表示することはできません – user2254968

+0

ああ。さて、これは、StackOverflowを使用してより良い応答を得ること、できるだけ具体的な問題として質問したり、コードの一部を投稿したりする手助けをする機会です。マントラは、あなたがあなたの質問を洗練しようとするときに、あなた自身で解決策を見つけるのが普通です。あなたは、ボタンプレスからの警告を表示するために特に検索しましたか? UIButtonからのUIAlertのグーグルリングはおそらく解決策を提供します。 – Ralfonso

答えて

0

は、カスタムテーブルビューのセルにコンテナView Controllerweakインスタンスを渡します。
セルにはが渡され、View Controllerとなり、UIAlertControllerと表示されます。
のように、これはなります達成するためにいくつかのサンプルコード:あなたはちょうどあなたがそれに表示するものは何でもラベルとボタンと1つのカスタムセルを作成する必要が

// CustomTableViewCell.h 

@interface CustomTableViewCell : UITableViewCell 

@property (nonatomic, weak) __kindof UIViewController *controllerDelegate; // __kindof used to avoid importing the View Controller class, we only need a controller object & nothing else, hence using __kindof will suffice 

@end 

// CustomTableViewCell.m 

@implementation CustomTableViewCell 

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) {  
     ... 
    } 
    return self; 
} 

- (void)setControllerDelegate:(__kindof UIViewController *)controllerDelegate{ 

    _controllerDelegate = controllerDelegate; 

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is an alert!" preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; 
    [alertController addAction:cancelButton]; 

    UIAlertAction *proceedButton = [UIAlertAction actionWithTitle:@"Proceed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
     ... // add custom actions on alert button tap here 
    }]; 
    [alertController addAction:proceedButton]; 

    [_controllerDelegate presentViewController:alertController animated:YES completion:nil]; 
} 

@end 


// MyViewController.m 

@interface MyViewController() 
... 
@end 

@implementation MyViewController 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:taskTableIdentifier forIndexPath:indexPath]; 

    cell.controllerDelegate = self; 

    return cell;   
} 

@end 
1

CustomTableViewCell.h

#import <UIKit/UIKit.h> 

@interface CustomTableViewCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UILabel *lblName; 
@property (weak, nonatomic) IBOutlet UIButton *btnAlert; 

@end 

CustomTableViewCell.h

#import "CustomTableViewCell.h" 

@implementation CustomTableViewCell 

- (void)awakeFromNib { 
    [super awakeFromNib]; 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

今、あなたのコントローラでは、あなたは、テーブルビューを作成して、カスタムセルを与え、ボタン方式をバインドする必要がありますin cellForRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return 10; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static N 

SString *simpleTableIdentifier = @"CustomTableViewCell"; 

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = (CustomTableViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.lblName.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; 
    cell.btnAlert.tag = indexPath.row; 
    [cell.btnAlert addTarget:self action:@selector(alertButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    return cell; 
} 

-(IBAction)alertButtonClicked:(id)sender 
{ 
    [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Button Clicked" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show]; 
} 
+0

@ user2254968解決策を手に入れましたか? – Nirmalsinh

関連する問題