115拆分表视图

效果如下:

115拆分表视图

ViewController.h

1 #import <UIKit/UIKit.h>

2 

3 @interface ViewController : UITableViewController

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

5 @property (strong, nonatomic) NSMutableArray *mArrBlueFlowerName;

6 

7 @end

ViewController.m

 1 #import "ViewController.h"

 2 

 3 @interface ViewController ()

 4 - (void)layoutUI;

 5 @end

 6 

 7 @implementation ViewController

 8 #define kFlowerCount 7

 9 #define kRedFlowerType 0

10 #define kBlueFlowerType 1

11 

12 - (void)viewDidLoad {

13     [super viewDidLoad];

14     

15     [self layoutUI];

16 }

17 

18 - (void)didReceiveMemoryWarning {

19     [super didReceiveMemoryWarning];

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

21 }

22 

23 - (void)layoutUI {

24     self.navigationItem.title = @"拆分表视图";

25     

26     _mArrRedFlowerName = [[NSMutableArray alloc] initWithCapacity:kFlowerCount];

27     _mArrBlueFlowerName = [[NSMutableArray alloc] initWithCapacity:kFlowerCount];

28     for (NSInteger i=1; i<=kFlowerCount; i++) {

29         [_mArrRedFlowerName addObject:[NSString stringWithFormat:@"RedFlower%ld", (long)i]];

30         [_mArrBlueFlowerName addObject:[NSString stringWithFormat:@"BlueFlower%ld", (long)i]];

31     }

32 }

33 

34 #pragma mark - TableView

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

36     NSString *strTitle = @"";

37     switch (section) {

38         case kRedFlowerType:

39             strTitle = @"红色花朵";

40             break;

41         case kBlueFlowerType:

42             strTitle = @"蓝色花朵";

43             break;

44     }

45     return strTitle;

46 }

47 

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

49     return 2;

50 }

51 

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

53     return kFlowerCount;

54 }

55 

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

57     static NSString *cellIdentifier = @"cellIdentifier";

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

59     if (!cell) {

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

61     }

62     

63     switch (indexPath.section) {

64         case kRedFlowerType:

65             cell.textLabel.text = _mArrRedFlowerName[indexPath.row];

66             break;

67         case kBlueFlowerType:

68             cell.textLabel.text = _mArrBlueFlowerName[indexPath.row];

69             break;

70     }

71     cell.imageView.image = [UIImage imageNamed:cell.textLabel.text];

72     return cell;

73 }

74 

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

76     NSString *strMessage = @"";

77     switch (indexPath.section) {

78         case kRedFlowerType:

79             strMessage = [NSString stringWithFormat:@"红色花朵:%@", _mArrRedFlowerName[indexPath.row]];

80             break;

81         case kBlueFlowerType:

82             strMessage = [NSString stringWithFormat:@"蓝色花朵:%@", _mArrBlueFlowerName[indexPath.row]];

83             break;

84     }

85     UIAlertView *alertVCustom = [[UIAlertView alloc] initWithTitle:@"选择信息"

86                                                            message:strMessage

87                                                           delegate:nil

88                                                  cancelButtonTitle:nil

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

90     [alertVCustom show];

91 }

92 

93 @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

 

你可能感兴趣的:(视图)