Created
November 15, 2017 09:12
-
-
Save wangdu1005/69654e68b11237ac25346f583f0f50e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "XXRootViewController.h" | |
@implementation XXRootViewController { | |
NSMutableArray *_objects; | |
} | |
- (void)loadView { | |
[super loadView]; | |
_objects = [[NSMutableArray alloc] init]; | |
self.title = @"Root View Controller"; | |
self.navigationItem.leftBarButtonItem = self.editButtonItem; | |
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)] autorelease]; | |
} | |
- (void)addButtonTapped:(id)sender { | |
[_objects insertObject:[NSDate date] atIndex:0]; | |
[self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:0 inSection:0] ] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
} | |
#pragma mark - Table View Data Source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return _objects.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (!cell) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
NSDate *date = _objects[indexPath.row]; | |
cell.textLabel.text = date.description; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { | |
[_objects removeObjectAtIndex:indexPath.row]; | |
[tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
} | |
#pragma mark - Table View Delegate | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment