博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableVIew and UITableViewController
阅读量:6617 次
发布时间:2019-06-25

本文共 5398 字,大约阅读时间需要 17 分钟。

UITableView is a view object, so, according to Model-View-Controller, it knows how to draw itself, but that's it.

UITableView needs a data source to ask for data, a delegate to inform events involving the UITableView, and typically a view controller to create and destroy instance of UITableView.

a singleton

#import 
@interface PossessionStore : NSObject{}// Notice that this is a class method, and is prefixed with a + instead of a -+ (PossessionStore *)defaultStore;@end

at the top of .m file, create a global static variable to hold the instance variable.

#import "PossessionStore.h"#import "Possession.h"static PossessionStore *defaultStore = nil;@implementation PossessionStore

Also, in .m file, implement +defaultStore, +allocWithZone: and -init so that only one instance of the class can be created.

+ (PossessionStore *)defaultStore{if (!defaultStore) {// Create the singletondefaultStore = [[super allocWithZone:NULL] init];}return defaultStore;}// Prevent creation of additional instances+ (id)allocWithZone:(NSZone *)zone{return [self defaultStore];}- (id)init{// If we already have an instance of PossessionStore...if (defaultStore) {// Return the old onereturn defaultStore;}self = [super init];return self;}

Now, override the retain count methods so that no one can release the defaultStore.

- (id)retain{// Do nothingreturn self;}- (void)release{// Do nothing}- (NSUInteger)retainCount{return NSUIntegerMax;}

Now you have a singleton.

Reuse UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{// Check for a reusable cell first, use that if it existsUITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];// If there is no reusable cell of this type, create a new oneif (!cell) {cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"UITableViewCell"] autorelease];}Possession *p = [[[PossessionStore defaultStore] allPossessions]objectAtIndex:[indexPath row]];[[cell textLabel] setText:[p description]];return cell;}

Editing Mode

UITableView has an editing property, and when this property is set to YES, the UITableView enters editing mode. Editing mode does not allow the user to edit the content of a row.

In the toggleEditingMode: method, you could toggle the editing property of UITableView directly. However, UITableViewController also has an editing property. A UITableViewController instance automatically sets the editing property of its table view to match its own editing property. Which one should you set? Follow the Model-View-Controller pattern: talke to the controller and let the controller talk to the view.

- (void)toggleEditingMode:(id)sender{// If we are currently in editing mode...if ([self isEditing]) {// Change text of button to inform user of state[sender setTitle:@"Edit" forState:UIControlStateNormal];// Turn off editing mode[self setEditing:NO animated:YES];} else {// Change text of button to inform user of state[sender setTitle:@"Done" forState:UIControlStateNormal];// Enter editing mode[self setEditing:YES animated:YES];}}

Adding Rows

We are going to put a New button in the header view to allow adding a row.

- (IBAction)addNewPossession:(id)sender{[[PossessionStore defaultStore] createPossession];// tableView returns the controller's view[[self tableView] reloadData];}

Deleting Rows

Before the table view will delete a row, it sends its data source a message about the deletion and waits for confirmation before pulling the trigger.

delete from datasource, not just from UITableViewController.

- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath{    // If the table view is asking to commit a delete command...   if (editingStyle == UITableViewCellEditingStyleDelete)   {       PossessionStore *ps = [PossessionStore defaultStore];       NSArray *possessions = [ps allPossessions];       Possession *p = [possessions objectAtIndex:[indexPath row]];       [ps removePossession:p];       // We also remove that row from the table view with an animation       [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]       withRowAnimation:YES];   }}

Moving Rows

you will use another method from the UITableViewDataSource protocol – tableView:moveRowAtIndexPath:toIndexPath:.

- (void)movePossessionAtIndex:(int)from                     toIndex:(int)to{  if (from == to) {    return;  }  // Get pointer to object being moved  Possession *p = [allPossessions objectAtIndex:from];  // Retain it... (retain count of p = 2)  [p retain];  // Remove p from array, it is automatically sent release (retain count of p = 1)  [allPossessions removeObjectAtIndex:from];  // Insert p in array at new location, retained by array (retain count of p = 2)  [allPossessions insertObject:p atIndex:to];  // Release p (retain count = 1, only owner is now array)  [p release];}

Now, in implement tableView:moveRowAtIndexPath:toIndexPath: to update the store.

- (void)tableView:(UITableView *)tableViewmoveRowAtIndexPath:(NSIndexPath *)fromIndexPathtoIndexPath:(NSIndexPath *)toIndexPath{    [[PossessionStore defaultStore] movePossessionAtIndex:[fromIndexPath row]                                                  toIndex:[toIndexPath row]];}

 

 

转载于:https://www.cnblogs.com/grep/archive/2012/06/16/2551692.html

你可能感兴趣的文章
Linux系统进程CPU使用率限制脚本
查看>>
我的友情链接
查看>>
STP理论知识
查看>>
mysql 将时间戳直接转换成日期时间
查看>>
linux笔记--DNS服务配置
查看>>
ubuntu13.04安装低版本的gcc和g++
查看>>
Linux常用命令1
查看>>
JS脚本强制kill掉MongoDB慢查询
查看>>
Bash on windows从14.0升级到ubuntu16.04
查看>>
分布式文件系统之管理DFS复制与基于访问的枚举
查看>>
iOS各种证书
查看>>
VC++编程之第二课笔记——C++的继承封装多态
查看>>
homework week03
查看>>
【Java例题】7.4 文件题1-学生成绩排序
查看>>
L3-004. 肿瘤诊断
查看>>
linux环境安装oracle11G准备环境(自学笔记1)
查看>>
js传输中文参数
查看>>
Windows sever 2008
查看>>
【C#】程序集中资源文件的使用
查看>>
docker基本使用
查看>>