iOS Programming 学习笔记 - 03 UITableView和UITableViewController
来源:互联网 发布:gson 源码 编辑:程序博客网 时间:2024/06/06 13:08
1. UITableViewController
MVC模式中Model用于存储数据,但是Model不需要了解user interface。View用于向用户进行显示,但View不需要了解Model对象。Controller用于同步user interface和model对象,并对程序进行控制。
因此,作为view的UITableView不需要处理应用程序逻辑或数据,当我们使用UITableView时,需要解决一下几个问题:
- 需要一个view controller来处理UITableView的显示问题。
- 需要为UITableView提供数据源。UITableView从数据源获取数据的行数,每行要显示的数据。
- 需要为UITableView提供用于处理特定事件的代理。
UITableViewController就用于解决上述的三个问题。UITableViewController和UITableView的关系如下:
接下来,我们创建自己的UITableViewController。
创建BNRItemsViewController继承于UITableViewController.
#import <UIKit/UIKit.h>@interface BNRItemsViewController : UITableViewController@end实现init和initWithStyle方法,设置style为plain
- (instancetype)init{ self = [super initWithStyle:UITableViewStylePlain]; return self;}- (instancetype)initWithStyle:(UITableViewStyle)style{ return [self init];}接下来,在appDelegate.m的didFinishLaunchWithOption:方法中创建TableViewController并将其设置为rootViewController
// Create a BNRItemViewController BNRItemsViewController *itemsViewController = [[BNRItemsViewController alloc] init]; // Place BNRItemsViewController's table view in the window hierarchy self.window.rootViewController = itemsViewController;编译并,执行,空得UITableView将被显示出来
2. Data Source
2.1 创建BNRItemStore
这里使用单例模式。
#import <Foundation/Foundation.h>@interface BNRItemStore : NSObject+ (instancetype)sharedStore;@endsharedStore是一个静态变量,默认值为nil。
#import "BNRItemStore.h"@implementation BNRItemStore+ (instancetype)sharedStore{ static BNRItemStore *sharedStore = nil; if (!sharedStore) { sharedStore = [[self alloc] initPrivate]; } return sharedStore;}// If a programmer calls [[BNRItemStore alloc] init], let him know the error of his ways- (instancetype)init{ @throw [NSException exceptionWithName:@"Singleton" reason:@"Use +[BNRItemStore sharedStore]" userInfo:nil]; return nil;}// Here is the real(secret) initializer- (instancetype)initPrivate{ self = [super init]; return self;}@endBNRItemsViewController通过向BNRItemStore发送消息来创建BNRItem。
修改BNRItemStore.h,声明只读的数组allItems和createItem接口:
@property (nonatomic, readonly) NSArray *allItems;- (BNRItem *)createItem;修改BNRItemStore.m,声明一个可修改的NSMutableArray privateItems,并实现createItem方法。同时修改allItems的get方法,让它返回privateItems。这样做的目的是,保证items数组只能被BNRItemStore类修改,外部程序只能通过特定的接口来修改它。
// Here is the real(secret) initializer- (instancetype)initPrivate{ self = [super init]; if (self) { _privateItems = [[NSMutableArray alloc] init]; } return self;}- (NSArray *)allItems{ return self.privateItems;}- (BNRItem *)createItem{ BNRItem *item = [BNRItem randomItem]; [self.privateItems addObject:item]; return item;}
2.2 使用data source
修改BNRItemsViewController,让其创建BNRItemStore并添加5个item到Store中:
- (instancetype)init{ self = [super initWithStyle:UITableViewStylePlain]; if (self) { for (int i = 0; i < 5; i++) { [[BNRItemStore sharedStore] createItem]; } } return self;}
声明BNRItemsViewController符合UITableViewDataSource协议,并实现相应的必需方法:
#import <UIKit/UIKit.h>@interface BNRItemsViewController : UITableViewController <UITableViewDataSource>@end
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [[[BNRItemStore sharedStore] allItems] count];}
3. UITableViewCell
UITableView的每一行都是一个view。这些view是UITableViewCell的实例,接下来我们为UITableView创建自定义的UITableViewCell。UITableViewCell的layout如下:
contentView由三个子view组成:两个UILabel(textLabel和detaiTextLabel)和一个UIImageView(imageview),关系图如下:
每个cell还有一个UITableViewCellStyle的属性用于设置cell的样式,取值类型如下:
3.1 创建和使用UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; NSArray *items = [[BNRItemStore sharedStore] allItems]; BNRItem *item = items[indexPath.row]; cell.textLabel.text = [item description]; return cell;}修改viewDidLoad方法,注册reuse identifier
- (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];}程序执行效果如下:
- iOS Programming 学习笔记 - 03 UITableView和UITableViewController
- ios开发 UITableViewController和 UITableView
- 黑马程序员-IOS学习笔记-UITableView、UITableViewCell、UITableViewController
- iOS基础--UITableView,UITableViewController
- IOS--UITableViewController及UITableView
- UIViewController+UITableView 和 UITableViewController
- iOS Programming 学习笔记 - 04 UITableView编辑模式
- iOS个人整理19-UITableViewController和UITableView的编辑
- IOS学习笔记8—UITableViewController
- iOS学习笔记03-UITableView
- ios - UITableViewController 笔记
- IOS UITableView 学习笔记
- iOS 学习笔记 uitableView
- iOS开发-Day30&31 UITableView&UITableViewController
- iOS学习笔记--03 UITableView相关
- 第十章——UITableView 和 UITableViewController
- iOS开发学习笔记 UITableview
- 转发iOS UITableView学习笔记
- windows注册表中数据库字符集被修改,数据库打不开处理过程。
- MySQL 1215 Cannot add foreign key constraint 错误解决办法
- iOS 之公共wifi解决方案
- 内核编译图文教程,usb无线网卡篇
- leetcode 日经贴,python code -repeated-dna-sequences
- iOS Programming 学习笔记 - 03 UITableView和UITableViewController
- RAC and Oracle Clusterware Best Practices and Starter Kit (Platform Independent) (文档 ID 810394.1)
- SVN迁移
- 最短路-Dijkstra算法
- poi解析Excel,存入MySQL
- vc读写xml
- Mysql 报错注入的原理探索
- UVa 12333 – Revenge of Fibonacci [大数+字典树]
- vc6 弹出对话框添加菜单,添加256真彩色工具栏