DetailViewController.m
上传用户:hechengdz
上传日期:2020-05-13
资源大小:1591k
文件大小:7k
源码类别:

iPhone

开发平台:

Objective-C

  1. /*
  2.      File: DetailViewController.m
  3.  Abstract: 
  4. Displays the details of a Book object and allows the user the edit them.
  5.   Version: 1.9
  6.  
  7.  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
  8.  Inc. ("Apple") in consideration of your agreement to the following
  9.  terms, and your use, installation, modification or redistribution of
  10.  this Apple software constitutes acceptance of these terms.  If you do
  11.  not agree with these terms, please do not use, install, modify or
  12.  redistribute this Apple software.
  13.  
  14.  In consideration of your agreement to abide by the following terms, and
  15.  subject to these terms, Apple grants you a personal, non-exclusive
  16.  license, under Apple's copyrights in this original Apple software (the
  17.  "Apple Software"), to use, reproduce, modify and redistribute the Apple
  18.  Software, with or without modifications, in source and/or binary forms;
  19.  provided that if you redistribute the Apple Software in its entirety and
  20.  without modifications, you must retain this notice and the following
  21.  text and disclaimers in all such redistributions of the Apple Software.
  22.  Neither the name, trademarks, service marks or logos of Apple Inc. may
  23.  be used to endorse or promote products derived from the Apple Software
  24.  without specific prior written permission from Apple.  Except as
  25.  expressly stated in this notice, no other rights or licenses, express or
  26.  implied, are granted by Apple herein, including but not limited to any
  27.  patent rights that may be infringed by your derivative works or by other
  28.  works in which the Apple Software may be incorporated.
  29.  
  30.  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
  31.  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  32.  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  33.  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  34.  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  35.  
  36.  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  37.  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38.  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39.  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  40.  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  41.  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  42.  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  43.  POSSIBILITY OF SUCH DAMAGE.
  44.  
  45.  Copyright (C) 2008 Apple Inc. All Rights Reserved.
  46.  
  47.  */
  48. #import "DetailViewController.h"
  49. #import "Book.h"
  50. #import "MasterViewController.h"
  51. #import "EditingViewController.h"
  52. #import "AppDelegate.h"
  53. @interface DetailViewController ()
  54. @property (nonatomic, retain) NSDateFormatter *dateFormatter;
  55. @property (nonatomic, retain) UITableView *tableView;
  56. @property (nonatomic, retain) NSIndexPath *selectedIndexPath;
  57. @end
  58. @implementation DetailViewController
  59. @synthesize book, dateFormatter, tableView, selectedIndexPath;
  60. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  61.     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  62.         // Title displayed by the navigation controller.
  63.         self.title = @"Info";
  64.         // Create a date formatter to convert the date to a string format.
  65.         dateFormatter = [[NSDateFormatter alloc] init];
  66.         [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  67.         [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
  68.     }
  69.     return self;
  70. }
  71. - (void)dealloc {
  72.     // Release owned resources.
  73.     [selectedIndexPath release];
  74.     [tableView release];
  75.     [book release];
  76.     [dateFormatter release];
  77.     [super dealloc];
  78. }
  79. - (void)viewDidLoad {
  80.     self.navigationItem.rightBarButtonItem = self.editButtonItem;
  81. }
  82. - (void)viewWillAppear:(BOOL)animated {
  83.     // Remove any existing selection.
  84.     [tableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
  85.     // Redisplay the data.
  86.     [tableView reloadData];
  87. }
  88. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  89.     [super setEditing:editing animated:animated];
  90.     [self.navigationItem setHidesBackButton:editing animated:animated];
  91.     [tableView reloadData];
  92. }
  93. #pragma mark -
  94. #pragma mark <UITableViewDelegate, UITableViewDataSource> Methods
  95. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tv {
  96.     // 3 sections, one for each property
  97.     return 3;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
  100.     // Only one row for each section
  101.     return 1;
  102. }
  103. - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  104.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
  105.     if (cell == nil) {
  106.         // Create a new cell. CGRectZero allows the cell to determine the appropriate size.
  107.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
  108.     }
  109.     switch (indexPath.section) {
  110.         case 0: cell.text = book.title; break;
  111.         case 1: cell.text = [dateFormatter stringFromDate:book.copyright]; break;
  112.         case 2: cell.text = book.author; break;
  113.     }
  114.     return cell;
  115. }
  116. - (NSString *)tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)section {
  117.     // Return the displayed title for the specified section.
  118.     switch (section) {
  119.         case 0: return @"Title";
  120.         case 1: return @"Copyright";
  121.         case 2: return @"Author";
  122.     }
  123.     return nil;
  124. }
  125. - (NSIndexPath *)tableView:(UITableView *)tv willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  126.     // Only allow selection if editing.
  127.     return (self.editing) ? indexPath : nil;
  128. }
  129. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  130.     EditingViewController *controller = [MasterViewController editingViewController];
  131.     
  132.     controller.editedObject = book;
  133.     switch (indexPath.section) {
  134.         case 0: {
  135.             controller.textValue = book.title;
  136.             controller.editedFieldKey = @"title";
  137.             controller.dateEditing = NO;
  138.         } break;
  139.         case 1: {
  140.             controller.dateValue = book.copyright;
  141.             controller.editedFieldKey = @"copyright";
  142.             controller.dateEditing = YES;
  143.         } break;
  144.         case 2: {
  145.             controller.textValue = book.author;
  146.             controller.editedFieldKey = @"author";
  147.             controller.dateEditing = NO;
  148.         } break;
  149.     }
  150.     self.selectedIndexPath = indexPath;
  151.     [self.navigationController pushViewController:controller animated:YES];
  152. }
  153. - (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
  154.     // Show the disclosure indicator if editing.
  155.     return (self.editing) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  156. }
  157. @end