标签:
一,tableview自带编辑模式,可以添加、删除、移动item
二,可以添加section或者table的header和footer
三,使用interface Builder创建header的layout
四,UITableView显示header前,会向它的controller发送headerVIew消息
- (UIView *)headerView
{
// If you have not loaded the headerView yet...
if (!_headerView) {
// Load HeaderView.xib
[[NSBundle mainBundle] loadNibNamed:@"HeaderView"
owner:self
options:nil];
}
return _headerView;
}在viewDidLoad中需要告诉tableView有headerVIew
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@"UITableViewCell"];
UIView *header = self.headerView;
[self.tableView setTableHeaderView:header];
}五,任意一个都可以是一个NIB的owner,都可以通过bundle的loadNibNamed:owner:options:加载一个NIB文件;
六,通过tableViewController的setEditing:animated:. 开启编辑模式;
七,添加一个item
操作dataSource的同时还要操作view
- (IBAction)addNewItem:(id)sender
{
NSInteger lastRow = [[self tableView] numberOfRowsInSection:0];
// Create a new BNRItem and add it to the store
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
// Figure out where that item is in the array
NSInteger lastRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
// Insert this new row into the table
[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationTop];
}八,删除一个item
1, tableView的编辑模式下,已经有delete按钮
2,实现dataSource的 tableView:commitEditingStyle:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
[[BNRItemStore sharedStore] removeItem:item];
// Also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}同样要先操作model,再操作tableView(无法和android一样自动同步?)
九,移动item
要实现moveItemAtIndex:toIndex:.;
只需要操作Model,无需操作tableView
标签:
原文地址:http://blog.csdn.net/xinzhou201/article/details/51351657