Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View) ใน iOS Application บน Xcode จะมี Template App ชื่อว่า Master-Detail Application โดย Template นี้จะเป็นตัวอย่าง App ที่อยู่ในรูปแบบของ Master และ Detail ซึ่งจะใช้ Table View (UITableView) สำหรับการ เพิ่ม/แสดง/ลบ ข้อมูล และสามารถที่จะคลิกที่ Cell ของแต่ล่ะ Row เพื่อส่งค่าไปยังหน้า Detail ได้ โดยใน Master-Detail Application สามารถเลือกสร้างได้ทั้งแบบ View ธรรมดา และ Storyboard View

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)


ใน Master-Detail Application นี้ถือว่าเป็นตัวอย่างสำหรับการเขียน App ที่มี 2 หน้าหรือ 2 View ได้อย่างดีเยี่ยมเลย เพราะเราจะได้เรียนรู้โครงสร้างการเขียนโปรแกรม และการส่งค่าระหว่าง View ซึ่งผมเองก็ได้ศึกษาตัวอย่างจาก Template Master-Detail Application เช่นเดียวกัน

Example ตัวอย่างการสร้าง Application แบบ Master-Detail Application

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

เริ่มต้นการสร้าง Application บน Xcode โดยเลือกแบบ Master-Detail Application

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

สามารถเลือกแบบใช้หรือไม่ใช้ Storyboard ก็ได้

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

หลังจากที่สร้าง Project เรียบร้อยแล้ว เราจะได้ไฟล์สำหรับ View ขึ้นมา 2 ชุดคือ MasterViewController.h / MasterViewController.m

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

และ DetailViewController.h / DetailViewController.m

เราไม่ต้องเขียน Code ใด ๆ เพิ่มทั้งสิ้น ก็สามารถที่จะทำการรัน Application ได้ทันที








Screenshot

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

หน้าแรกของ App ให้คลิกที่ + (บวก) เพื่อเพิ่มข้อมูล

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

คลิกที่ Edit เพื่อเข้าสู่โหมดสำหรับการแก้ไขลบข้อมูล

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

เข้าสู่โหมดสำหรับการแก้ไขลบข้อมูล

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

คลิกที่ลบข้อมูล หรือ Delete

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

หรือจะใช้การ Swipe หรือ Slide Cell เพื่อแสดง Delete หรือจะคลิกที่ Done เพื่อเข้าสู่โหมดปกติ

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

คลิกที่ Cell หรือ Rows เพื่อไปยังหน้า Detail

iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)

แสดงหน้า Detail

เพิ่มเติม จากบทความนี้จะเห็นว่า Master-Detail Application ได้สร้าง View ให้เราขึ้นมา 2 View รวมทั้งตัวอย่าง Code การแสดง Mode สำหรับ Delete และการใช้แบบ Master Detail ซึ่งทั้งหมดนี้สามารถเป็นตัวอย่างได้อย่างดี

Code ทั้งหมดของ .h และ .m ในภาษา Objective-C

MasterViewController.h
//
//  MasterViewController.h
//  masterDetailApp
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) DetailViewController *detailViewController;

@end









MasterViewController.m
//
//  MasterViewController.m
//  masterDetailApp
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
    return self;
}
							
- (void)dealloc
{
    [_detailViewController release];
    [_objects release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.detailViewController) {
        self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
    }
    NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = object;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

@end


DetailViewController.h
//
//  DetailViewController.h
//  masterDetailApp
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

@property (strong, nonatomic) id detailItem;

@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;

@end


DetailViewController.m
//
//  DetailViewController.m
//  masterDetailApp
//
//  Created by Weerachai on 12/10/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

- (void)dealloc
{
    [_detailItem release];
    [_detailDescriptionLabel release];
    [super dealloc];
}

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        [_detailItem release];
        _detailItem = [newDetailItem retain];

        // Update the view.
        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Detail", @"Detail");
    }
    return self;
}
							
@end


โดย Code ทั้งหมดนี้เป็น Code ที่โปรแกรมได้ทำการสร้างมาให้อัตโนมัติ


.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-12-12 08:49:59 / 2017-03-26 09:45:10
  Download : Download  iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)
 Sponsored Links / Related

 
iOS/iPhone Multiple View (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Change View Startup Open First (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Storyboard and View , Multiple View (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone TableView and UITableView (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Passing Data Between View (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone TableView Passing Data to Another View (Objective-C,iPhone,iPad)
Rating :

 
iOS/iPhone Splash Screen Launch and Apps icons Launcher Example (iPhone,iPad)
Rating :

 
iOS/iPhone Table View Master-Detail and Passing Data (Objective-C,iPhone)
Rating :

 
iOS/iPhone Portrait and Landscape Orientation (Objective-C, iPhone, iPad)
Rating :

 
iOS/iPhone Table View Static Cell and Section Group (iPhone, iPad)
Rating :

 
iOS/iPhone Table View(UITableView) Sections from an NSArray/NSMutableArray/NSDictionary (Objective-C,iPhone, iPad)
Rating :

 
iOS/iPhone Table View Image Column Multiple Column (Objective-C,iPhone)
Rating :

 
iOS/iPhone Table View and Table View Cell - Custom Cell Column (iPhone, iPad)
Rating :

 
iOS/iPhone Table View from NSDictionary - UITableview and NSMutableDictionary
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่