2017-03-02 60 views
2

我想用自定義單元格中的按鈕顯示警報。我如何在Objective C中做到這一點? 由於顯示來自UITableViewCell類的警報 - Objective c

+0

你試過用谷歌搜索嗎? 「objective-c button tableview」提供了很多可能有用的結果。 – Ralfonso

+0

我知道如何添加按鈕,但我不能顯示警報 – user2254968

+0

噢好的。那麼,這是一個幫助您使用StackOverflow的機會 - 然後 - 獲得更好的響應,嘗試儘可能地詢問具體問題,併發布一些代碼。口頭禪是,當你試圖改進你的問題時,你通常會自己找到解決方案。你是否專門搜索顯示按鈕按下的警報?使用谷歌搜索「UIButton的UIAlert」可能會提供一個解決方案。 – Ralfonso

回答

0

傳遞的容器View Controller一個weak實例的定製表格視圖細胞。
手機將使用此傳入View Controller顯示UIAlertController
一些示例代碼來實現,這將是這樣的:

// 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 

現在,在你的控制器,你需要創建表視圖,讓您的自定義單元格和綁定按鈕方法在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