TableView动态的删除,添加cell

#import "ViewController.h"
#define NAVIGATION_HEIGHT 44

@interface ViewController (private)
- (void)initTableView;
- (void)initDataArray;
- (void)initNavigationBarButton;
- (void)addOneCell:(id)sender;
- (void)removeOneCell:(id)sender;
@end

@implementation ViewController
@synthesize myTableView,myDataArr;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initNavigationBarButton];
[self initDataArray];
[self initTableView];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Initialize Method
- (void)initTableView
{
UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - NAVIGATION_HEIGHT) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
UIView *footerView = [[UIView alloc] init];
[_tableView setTableFooterView:footerView];
[footerView release];
self.myTableView = _tableView;
[_tableView release];

[self.view addSubview:self.myTableView];
}

- (void)initDataArray
{
NSMutableArray *_tempArr = [NSMutableArray arrayWithCapacity:0];
for (int a = 0; a < 10; a++) {
NSString *str = [NSString stringWithFormat:@"num:%d",a];
[_tempArr addObject:str];
}
self.myDataArr = _tempArr;
}

- (void)initNavigationBarButton
{
UIBarButtonItem *_addBarButton = [[UIBarButtonItem alloc] initWithTitle:@"add" style:UIBarButtonItemStylePlain target:self action:@selector(addOneCell:)];
self.navigationItem.leftBarButtonItem = _addBarButton;
[_addBarButton release];

UIBarButtonItem *_removeBarButton = [[UIBarButtonItem alloc] initWithTitle:@"remove" style:UIBarButtonItemStylePlain target:self action:@selector(removeOneCell:)];
self.navigationItem.rightBarButtonItem = _removeBarButton;
[_removeBarButton release];
}

#pragma mark - TableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myDataArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = @"Animation insert";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
}
cell.textLabel.text = [self.myDataArr objectAtIndex:indexPath.row];
return cell;
}

#pragma mark - ButtonClick Method
- (void)addOneCell:(id)sender
{
[self.myTableView beginUpdates];
[self.myDataArr insertObject:@"new" atIndex:0];
NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationNone];
[self.myTableView endUpdates];
}

- (void)removeOneCell:(id)sender
{
[self.myTableView beginUpdates];
[self.myDataArr removeObjectAtIndex:0];
NSArray *_tempIndexPathArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.myTableView deleteRowsAtIndexPaths:_tempIndexPathArr withRowAnimation:UITableViewRowAnimationFade];
[self.myTableView endUpdates];
}

#pragma mark - Dealloc
- (void)dealloc
{
self.myTableView = nil;
self.myDataArr = nil;
[super dealloc];
}

@end

posted @ 2013-05-03 23:08  小乐"  阅读(338)  评论(0编辑  收藏  举报