iOS开发之-Table View-Data Hierarchy

Extension to the NSIndexPath Class

// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView)

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

@property(nonatomic,readonly) NSUInteger section;
@property(nonatomic,readonly) NSUInteger row;

@end

Navigating a Data Hierarchy With Table Views


Hierarchical Data Models and Table Views

1.The data model has hierarchical depth, and objects at various levels of this hierarchy should be the source for populating the rows of a table view.


The Data Model as a Hierarchy of Model Objects


1.You can describe model objects in terms of their properties. These properties are of two general kinds: attributes and relationships.


Attributes represent elements of model-object data.


A model object may also have relationships with other model objects, and it is through these relationships that a data model acquires hierarchical depth by composing an object graph. Relationships are of two general kinds in terms of cardinality: to-one and to-many. To-one relationships define an object’s relationship with another object (for example, a parent relationship). A to-many relationship on the other hand defines an object’s relationship with multiple objects of the same kind. The to-many relationship is characterized by containment and can be programmatically represented bycollections such as NSArray objects (or, simply, arrays). By collections thus nesting other collections, your data model can acquire hierarchical depth.


Table Views and the Data Model





View Controllers and Navigation-Based Applications




Table-View Controllers


You create a table-view controller by allocating memory for it and “Initialization” it with the initWithStyle: method, passing in either UITableViewStylePlain or UITableViewStyleGrouped for the required type of table view. Once you create a table-view controller, it either creates its table view or loads it from anib file. In either case, the behavior is slightly different:

  • If a nib file is specified, the UITableViewController object loads the UITableView object archived in the nib file. The table view’s attributes, size, and autoresizing characteristics are usually set in the nib file. Thedata source and delegate of the table view become those objects defined in the nib file, if any.
  • If there is no nib file, theUITableViewController object allocates and initializes an unconfiguredUITableView object with the correct dimensions and autoresize mask. It sets itself as the data source and the delegate of the table view; it also does this if the nib file defines no data source or delegate.


When the table view is about to appear for the first time, the table-view controller sendsreloadData to the table view, which prompts it to request data from its data source.

The UITableViewController class also performs other common tasks. It clears selections when the table view is about to be displayed and flashes the scroll indicators when the table finishes displaying. In addition, it responds properly when users tap the Edit button by putting the table view into editing mode. 


Note: You should use aUIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, one of which is a table view. The default behavior of theUITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present).


If you decide to use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view, you should perform a couple of the tasks mentioned above to conform to the human-interface guidelines. To clear any selection in the table view before it’s displayed, implement the viewWillAppear: method to clear the selected row (if any) by callingdeselectRowAtIndexPath:animated:. After the table view has been displayed, you should flash the scroll view’s scroll indicators by sending aflashScrollIndicators message to the table view; you can do this in an override of theviewDidAppear: method of UIViewController.


Managing Table Views In a Navigation-Based Application


Listing 3-1  Setting up the root view controller—window in nib file
- (void)applicationDidFinishLaunching:(UIApplication *)application {

    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

    NSArray *timeZones = [NSTimeZone knownTimeZoneNames];

    rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    self.navigationController = aNavigationController;

    [aNavigationController release];

    [rootViewController release];


    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

}


Listing 3-2  Setting up the root view controller—window created programmatically
- (void)applicationDidFinishLaunching:(UIApplication *)application {

 

    // Create the window

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

 

    // Create the navigation and view controllers

    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

    navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    [rootViewController release];

 

    // Configure and show the window

    [window addSubview:[navigationController view]];

    [window makeKeyAndVisible];

}

When you initialize a table-view controller with initWithStyle:, that “Initialization” is invoked as well as the initializers inherited from superclasses:

  • initWithNibName:bundle:—inherited from UIViewControllerIf your table view is defined in a nib file, instead of setting the Nib Name attribute of the table-view controller (as File’s Owner) in Interface Builder, you can override this method to call super, supplying the superclass with the name of the nib file.
  • init—inherited from NSObject

Listing 3-4  Setting the buttons of a navigation bar in loadView
- (void)loadView {

    [super viewDidLoad];

    self.tableView.allowsSelectionDuringEditing = YES;

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

 

    UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc]

        initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem:)];

    self.navigationItem.rightBarButtonItem = addButtonItem;

    [addButtonItem release];

}


Listing 3-5  Creating and pushing the next table-view controller on the stack
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    BATDetailViewController *trailDetailController = [[BATDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
    trailDetailController.curTrail = [trails objectAtIndex:indexPath.row];
    [[self navigationController] pushViewController:trailDetailController animated:YES];
    [trailDetailController release];
}


Design Pattern for Navigation-Based Applications

  • The view controller stores the data it needs for populating its table view. 
  • View controllers should not obtain the data for their table view through a global variable or asingleton object such as the application delegate. Such direct dependencies make your code less reusable and more difficult to test and debug. 
  • The current view controller on top of the navigation-controller stack creates the next view controller in the sequence and, before it pushes it onto the stack, sets the data this view controller, acting as data source, needs to populate its table view.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值