1.UITableViewController
- a UITableView needs a view to handle its appearance on the screen
- a UITableView needs a data source. ask for the number of rows to display
- a UITableView needs a delegate.it can be any object that conforms the UITableViewDelegate
= an instance of the class UITableViewController can fill all three roles:controller,datasource,delegate
当controller创建了它的view[都是uitableview或子类]的时候,view会自动将dataSource和delegate指向controller。
2.UITableViewController子类
指定初始化函数:initWithStyle:(UITableViewStyle)style
你可以重写初始化函数
(instance)init
{
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
(instance)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
以上可以确保table是plain类型的
3.UITableView's Data Source
显示数据的处理:
一般都是你告诉列表改怎么显示,
这里是table view去问dataSource属性,该如何显示,而dataSource就是对应的controller,所以controller应该存储显示子项数据
使用BNRItemStore来封装数据,带有加载,删除等数据操作功能
单例模式
+(BNRItemStore *)sharedStore
{
static BNRItemStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}
-(instancetype)init
{
@throw [NSException exceptionWithName:@"singleton"
reason:@"Use + [NSBRItemStore sharedStore]"
userInfo:nil];
return nil;
}
-(BNRItemStore *)initPrivate
{
return [super init];
}
4.@class
#import <Foundation/Foundation.h>
@class BNRItem;
@interface BNRItemStore : NSObject
@property (nonatomic,readonly)NSArray *allItems;
+(BNRItemStore *)sharedStore;
-(BNRItem *)createItem;
@end
@class告诉编译器 这儿有一个BNRItem类,并不需要知道它的细节,这儿用来声明createItem,不需要发送信息给它的对象或者它自身。 这样在编译上更快些。 当你在.m中需要创建它的实例时,就要导入BNRItem.h文件
5.对数据的掌控 !!!
在头文件中公开声明
@interface (nonatomic,readonly) NSArray *allItems;
在实现文件中,你可以声明一个私有的privateItems,
重写allItems的getter函数,
(NSArray *)allItem
{
return privateItems;
}
这样保持数据的控制,也没必要在有文件声明 NSMutableArray。
注意此时没有_allItems 变量。
6.editing mode
设置editing mode 属性为true,可以对每个项编辑,标记完成等操作
每个section都可以有footer和header,为UIView子类即可
7.xib文件。
一般来说都是用来给viewController创建view,但是也可以定义布局自己的view对象,
设置xib 布局的大小,在sumulated metrics的size位置设置为none。因为这种作为uitableviewcell的view并不需要很大。
新版的xcdoe是Freeform
设置File's Owner对应的class为BNRItemsViewController,说明是归它??
!!!一般来说,xib经常被用作创建一个视图控制器 就像BNRReminderViewController.xib
当然,也可以被用来当你希望组织一些视图对象的时候,指定它的file's owner给对应的viewController,同时在viewController中或者其他地方? 发送消息 loadNibNamed:owner:option: 给 应用的bundle。
对于用于viewController的xib文件,也是用同样的代码,只是在内部就指定了view,大概:
-(void)viewDidLoad
{
//[super viewDidLoad];
NSBundle *bundle = [self binBundle];
if(!bundle){
bundle = [NSBundle mainBundle];
}
NSString *binName = [self nibName];
if(!nibName){
nibName = NSStringFromClass([self class]);
}
NSString *nibPath = [bundle pathForResource:nibName ofType:@"nib"];
if(nibPath){
[bundle loadNibNamed:nibName owner:self option:nil];
}else{
self.view = [[UIView alloc] init];// 指定默认view
}
}
8.NSBundle
当你想访问程序的文件[bundle?].使用NSBundle:
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self option:nil];
是NSBundle加载XIB文件,注意没必要加后缀
设置owner:self 表示当解析NIBfile的时候,任何链接到file's onwer都会指向self的对象
9.指定header
一般在viewDidLoad中设置:
[self.tableView setTableHeaderView:self.header];
10.设置编辑与完成编辑,比如删除单元格之类的~
-(IBAction)toggleEditingMode:(UIButton *)sender
{
if(self.isEditing){
[sender setTitle:@"Edit" forState:UIControlStateNormal];
[self setEditing:NO animated:YES];
}else{
[sender setTitle:@"Done" forState:UIControlStateNormal];
[self setEditing:YES animated:YES];
}
}
11.添加单元格
记住:视图是根据数据来显示的,单单改变视图的做法不是很实用。
NBRItem *newItem = [[NBRItemStore sharedStore] createItem];
NSInteger lastRow = [[[NBRItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
[self.viewTable insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
12.移除单元格
当按下删除按钮 时候,tableView发送一个消息给data souce打算删除这个单元格,并且等待确认消息。
重写tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete){
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
[[BNRItemStore sharedStore] removeItem:item]; // use:removeObjectIdenticalTo or removeObject:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]
}
}
13.移动单元格
在可编辑状态下可移动单元格,不像删除单元格那样需要获得批准。你可以在移除的函数中更改model,这样之后就会根据model重新排序单元格。
//model 中的修改数据
-(void)moveItemAtIndex:(UIInteger)fromIndex toIndex:(UIInteger)toIndex
{
if(fromIndex == toIndex)
{
return;
}
BNRItem *item = self.privateItems[fromIndex];
[self.privateItems removeObjectAtIndex:fromIndex];
[self.privateItems insertObject:item atIndex:toIndex];
}
// table view
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndex:(NSIndexPath *)destinationIndexPath
{
[[BNRItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}