|
| 1 | +// |
| 2 | +// MainViewController.m |
| 3 | +// iOSDemoObjC |
| 4 | +// |
| 5 | + |
| 6 | +#import "MainViewController.h" |
| 7 | +#import "StreamBufferDemoViewController.h" |
| 8 | +#import "SSEParserDemoViewController.h" |
| 9 | +#import "UTF8SafetyDemoViewController.h" |
| 10 | +#import "SocketConnectionDemoViewController.h" |
| 11 | + |
| 12 | +typedef NS_ENUM(NSInteger, DemoSection) { |
| 13 | + DemoSectionCore = 0, |
| 14 | + DemoSectionSocket, |
| 15 | + DemoSectionCount |
| 16 | +}; |
| 17 | + |
| 18 | +@implementation MainViewController |
| 19 | + |
| 20 | +- (void)viewDidLoad { |
| 21 | + [super viewDidLoad]; |
| 22 | + self.title = @"NWAsyncSocket ObjC Demo"; |
| 23 | +} |
| 24 | + |
| 25 | +#pragma mark - UITableViewDataSource |
| 26 | + |
| 27 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 28 | + return DemoSectionCount; |
| 29 | +} |
| 30 | + |
| 31 | +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 32 | + switch (section) { |
| 33 | + case DemoSectionCore: return @"Core Components"; |
| 34 | + case DemoSectionSocket: return @"Socket"; |
| 35 | + default: return nil; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 40 | + switch (section) { |
| 41 | + case DemoSectionCore: return 3; |
| 42 | + case DemoSectionSocket: return 1; |
| 43 | + default: return 0; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 48 | + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; |
| 49 | + if (!cell) { |
| 50 | + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; |
| 51 | + cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 52 | + } |
| 53 | + |
| 54 | + UIListContentConfiguration *config = [UIListContentConfiguration cellConfiguration]; |
| 55 | + |
| 56 | + if (indexPath.section == DemoSectionCore) { |
| 57 | + switch (indexPath.row) { |
| 58 | + case 0: |
| 59 | + config.text = @"StreamBuffer"; |
| 60 | + config.image = [UIImage systemImageNamed:@"arrow.left.arrow.right"]; |
| 61 | + break; |
| 62 | + case 1: |
| 63 | + config.text = @"SSE Parser"; |
| 64 | + config.image = [UIImage systemImageNamed:@"antenna.radiowaves.left.and.right"]; |
| 65 | + break; |
| 66 | + case 2: |
| 67 | + config.text = @"UTF-8 Safety"; |
| 68 | + config.image = [UIImage systemImageNamed:@"textformat"]; |
| 69 | + break; |
| 70 | + } |
| 71 | + } else { |
| 72 | + config.text = @"Socket Connection"; |
| 73 | + config.image = [UIImage systemImageNamed:@"network"]; |
| 74 | + } |
| 75 | + |
| 76 | + cell.contentConfiguration = config; |
| 77 | + return cell; |
| 78 | +} |
| 79 | + |
| 80 | +#pragma mark - UITableViewDelegate |
| 81 | + |
| 82 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 83 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 84 | + |
| 85 | + UIViewController *vc = nil; |
| 86 | + if (indexPath.section == DemoSectionCore) { |
| 87 | + switch (indexPath.row) { |
| 88 | + case 0: |
| 89 | + vc = [[StreamBufferDemoViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 90 | + break; |
| 91 | + case 1: |
| 92 | + vc = [[SSEParserDemoViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 93 | + break; |
| 94 | + case 2: |
| 95 | + vc = [[UTF8SafetyDemoViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 96 | + break; |
| 97 | + } |
| 98 | + } else { |
| 99 | + vc = [[SocketConnectionDemoViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
| 100 | + } |
| 101 | + |
| 102 | + if (vc) { |
| 103 | + [self.navigationController pushViewController:vc animated:YES]; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +@end |
0 commit comments