128在单元格中添加自定义的辅助按钮

效果如下:

128在单元格中添加自定义的辅助按钮

ViewController.h

1 #import <UIKit/UIKit.h>

2 

3 @interface ViewController : UITableViewController

4 @property (strong, nonatomic) NSMutableArray *mArrDataSource;

5 

6 @end

ViewController.m

  1 #import "ViewController.h"

  2 

  3 @interface ViewController ()

  4 - (void)layoutUI;

  5 - (void)editingSwitchDidPush:(UIBarButtonItem *)sender;

  6 - (void)showAlert:(NSString *)msg;

  7 - (void)accessoryViewDidPush:(UIButton *)sender;

  8 - (void)editingAccessoryViewDidPush:(UIButton *)sender;

  9 @end

 10 

 11 @implementation ViewController

 12 

 13 - (void)viewDidLoad {

 14     [super viewDidLoad];

 15     

 16     [self layoutUI];

 17 }

 18 

 19 - (void)didReceiveMemoryWarning {

 20     [super didReceiveMemoryWarning];

 21     // Dispose of any resources that can be recreated.

 22 }

 23 

 24 - (void)layoutUI {

 25     _mArrDataSource = [[NSMutableArray alloc] initWithObjects:@"A", @"B", @"C", @"D", nil];

 26     

 27     self.navigationItem.title = @"在单元格中添加自定义的辅助按钮";

 28     UIBarButtonItem *barBtnEditingSwitch = [[UIBarButtonItem alloc] initWithTitle:@"编辑"

 29                                                                             style:UIBarButtonItemStylePlain

 30                                                                            target:self

 31                                                                            action:@selector(editingSwitchDidPush:)];

 32     self.navigationItem.rightBarButtonItem = barBtnEditingSwitch;

 33 }

 34 

 35 - (void)editingSwitchDidPush:(UIBarButtonItem *)sender {

 36     if ([sender.title isEqualToString:@"编辑"]) {

 37         sender.title = @"完成";

 38         self.tableView.editing = YES;

 39     } else {

 40         sender.title = @"编辑";

 41         self.tableView.editing = NO;

 42     }

 43 }

 44 

 45 - (void)showAlert:(NSString *)msg {

 46     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"

 47                                                     message:msg

 48                                                    delegate:self

 49                                           cancelButtonTitle:nil

 50                                           otherButtonTitles:@"确定", nil];

 51     [alert show];

 52 }

 53 

 54 - (void)accessoryViewDidPush:(UIButton *)sender {

 55     NSString *strMessage = [NSString stringWithFormat:@"普通状态下,您选择的是第%ld行的自定义辅助按钮", (sender.tag + 1)];

 56     [self showAlert:strMessage];

 57 }

 58 

 59 - (void)editingAccessoryViewDidPush:(UIButton *)sender {

 60     NSString *strMessage = [NSString stringWithFormat:@"编辑状态下,您选择的是第%ld行的自定义辅助按钮", (sender.tag + 1)];

 61     [self showAlert:strMessage];

 62 }

 63 

 64 #pragma mark - TableView

 65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

 66     return @"列表";

 67 }

 68 

 69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 70     return 1;

 71 }

 72 

 73 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 74     return _mArrDataSource.count;

 75 }

 76 

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

 78     static NSString *cellIdentifier = @"cellIdentifier";

 79     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 80     if (!cell) {

 81         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

 82     }

 83     

 84     cell.textLabel.text = _mArrDataSource[indexPath.row];

 85     //设置普通状态下,显示的自定义的辅助按钮;

 86     UIButton *btnAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoDark];

 87     btnAccessoryView.tag = indexPath.row;

 88     [btnAccessoryView addTarget:self

 89                          action:@selector(accessoryViewDidPush:)

 90                forControlEvents:UIControlEventTouchUpInside];

 91     cell.accessoryView = btnAccessoryView;

 92     

 93     //设置编辑状态下,显示的自定义的辅助按钮;同理:cell.accessoryType对应cell.editingAccessoryType

 94     UIButton *btnEditingAccessoryView = [UIButton buttonWithType:UIButtonTypeCustom];

 95     btnEditingAccessoryView.frame = CGRectMake(0, 0, 24, 19);

 96     [btnEditingAccessoryView setBackgroundImage:[UIImage imageNamed:@"Check"] forState:UIControlStateNormal];

 97     btnEditingAccessoryView.tag = indexPath.row;

 98     [btnEditingAccessoryView addTarget:self

 99                                 action:@selector(editingAccessoryViewDidPush:)

100                       forControlEvents:UIControlEventTouchUpInside];

101     cell.editingAccessoryView = btnEditingAccessoryView;

102     return cell;

103 }

104 

105 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

106     

107 }

108 

109 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>

2 

3 @interface AppDelegate : UIResponder <UIApplicationDelegate>

4 @property (strong, nonatomic) UIWindow *window;

5 @property (strong, nonatomic) UINavigationController *navigationController;

6 

7 @end

AppDelegate.m

 1 #import "AppDelegate.h"

 2 #import "ViewController.h"

 3 

 4 @interface AppDelegate ()

 5 @end

 6 

 7 @implementation AppDelegate

 8 

 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

11     ViewController *viewController = [[ViewController alloc] init];

12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

13     _window.rootViewController = _navigationController;

14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无

15     [_window makeKeyAndVisible];

16     return YES;

17 }

18 

19 - (void)applicationWillResignActive:(UIApplication *)application {

20 }

21 

22 - (void)applicationDidEnterBackground:(UIApplication *)application {

23 }

24 

25 - (void)applicationWillEnterForeground:(UIApplication *)application {

26 }

27 

28 - (void)applicationDidBecomeActive:(UIApplication *)application {

29 }

30 

31 - (void)applicationWillTerminate:(UIApplication *)application {

32 }

33 

34 @end

 

你可能感兴趣的:(自定义)