2017-03-02 26 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