Swift 学习笔记 (三) —— Table view徜徉在Data Hierarchy的海洋

Table View通常都会包含各种数据组成的层次结构中, 以下教你UIKit框架如何实现这些层次化的数据层结构。


Hierarchical Data Models and Table Views (海尔兄弟)


Model layer常用的技术有: Core Data, property list,  archives


Model 层model object通常有两类属性 attributes (树叶) relationships(树枝)


Table Views and Data Model


Table Viewd的Data Model由collection objects搭建, 当你创建table view它会通过data source察看其层次结构,多少sections和每个section多少row, 然后查看每个row的内容


data source通过这样的层次架构获取数据. 


data source和delegate通过传递 index来定位信息,(index 来自Foundation的NSIndexPath, 并由UIKit extend了 section & row 属性)


In the sequence of table views in Figure 3-1, the top level of the data hierarchy is an array of four arrays, with each inner array containing objects representing the trails for a particular region. When the user selects one of these regions, the next table view lists names identifying the trails within the selected array. When the user selects a particular trail, the next table view describes that trail using a grouped table view.

Figure 3-1  Mapping levels of the data model to table views


Swift 学习笔记 (三) —— Table view徜徉在Data Hierarchy的海洋_第1张图片


View Controllers And Navigation-Based Apps 


UIKit提供的View Controllers 都是 UIViewControllers的私生子。


UINavigationContoller是继承自UIViewController,它管理一stack的View Contgroller(先进后出), 它包含并控制一个UINavigationBar


Swift 学习笔记 (三) —— Table view徜徉在Data Hierarchy的海洋_第2张图片



Navigation Bars


包含一个title和按钮(供返回等功能)


Swift 学习笔记 (三) —— Table view徜徉在Data Hierarchy的海洋_第3张图片



UITableViewController


两种办法: storyBoard 或者 用程序 initWithStyle:   (UITableViewStylePlain or UITableViewStyleGrouped)


当table view第一次被调用, table view controller发送 reloadData 方法给 table view,致使table view 从data source 取数据


UITableViewController同样支持清理数据,管理数据,编辑数据等操作,还支持NSFetchedResultsController(管理Core Data fetch request的结果)


UITableViewController实现了UIViewController的loadView, viewWillApear等方法


你可以创建UITableViewController的儿子并重写这些方法


Note: You should use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, only 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 a flashScrollIndicators message to the table view; you can do this in an override of theviewDidAppear: method of UIViewController.


Managing Table Views in a Navigation-Based App(可以游泳的APP)


用户点其中一行时,delegate会调用 tableView: didSelectRowAtIndexPath: or tableView: accessoryButtonTappedForRowWithIndexPath:



Swift 学习笔记 (三) —— Table view徜徉在Data Hierarchy的海洋_第4张图片


在story board上scene and viewcontroller是同义的



Listing 3-1  Passing data to a destination view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
    MyDetailViewController *detailViewController = [segue destinationViewController];
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    detailViewController.data = [self.dataController objectInListAtIndex:indexPath.row];
  }
}

Listing 3-2  Passing data to a source view controller

@protocol MyAddViewControllerDelegate 
- (void)addViewControllerDidCancel:(MyAddViewController *)controller;
- (void)addViewControllerDidFinish:(MyAddViewController *)controller data:(NSString *)item;
@end
 
- (void)addViewControllerDidCancel:(MyAddViewController *)controller {
  [self dismissViewControllerAnimated:YES completion:NULL];
}
 
- (void)addViewControllerDidFinish:(MyAddViewController *)controller data:(NSString *)item {
  if ([item length]) {
    [self.dataController addData:item];
    [[self tableView] reloadData];
  }
  [self dismissViewControllerAnimated:YES completion:NULL];
}

Design Pattern for Navigation-Based Apps

A navigation-based app with table views should follow these design best practices:

  • view controller (typically a subclass of UITableViewController), acting in the role of data source, populates its table view with data from an object representing a level of the data hierarchy.

    When the table view displays a list of items, the object is typically an array. When the table view displays item detail (that is, a leaf node of the data hierarchy), the object can be a custom model object, a Core Data managed object, a dictionary, or something similar.

  • The view controller stores the data it needs for populating its table view.

    The view controller can use this data directly for populating the table view, or it can use it to fetch or otherwise obtain the necessary data. When you design your view controller subclass, you should define a property to hold this data.

    View controllers should not obtain the data for their table view through a global variable or a singleton object such as the appdelegate. 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 that this view controller, acting as data source, needs to populate its table view.





你可能感兴趣的:(swift_UI)