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 TableView Passing Data to Another View (Objective-C,iPhone,iPad)

iOS/iPhone TableView Passing Data to Another View (Objective-C,iPhone,iPad) บทความนี้จะเป็นการเขียน iOS บน iPhone ทำงานร่วมกับ Table View และการส่งค่าระหว่าง View โดยจะใช้การออกแบบ View ขึ้นมา 2 ตัว View แรกจะใช้ Table View แสดงข้อมูลจาก Array และเมื่อผู้ใช้คลิกที่ Item Rows ของ Table View ใน View แรก ก็จะมีการส่งค่าไปยัง View ที่สอง พร้อม ๆ กับการแสดงค่าบน Label ของ View ที่สอง โดยทั้งหมดนี้ใช้ Xcode และภาษา Objective-C

iOS/iPhone TableView  Passing Data to Another View

iOS/iPhone TableView Passing Data to Another View


ในการรับส่งค่าจาก Table View ไปยังอีก View หนึ่งนั้นจะใช้หลักการเช่นเดียวกับการส่งค่าปกติ คือขั้นแรกจะต้องอ่าน Item ที่ได้จากการเคลิกเลือกใน Rows ของ Table View จากนั้น สามารถใช้การเรียก Class ของ View ปลายทาง และใช้การส่งค่าผ่าน property ทีได้ประกาศไว้ สามารถอ่านได้จากหัวข้อก่อนหน้านี้



และบทความนี้อาจจะข้ามพวกขั้นตอนการสร้าง IBOutlet และ IBAction เพราะฉะนั้นควรศึกษาพื้นฐานให้แน่นซะก่อน ถึงจะสามารถเข้าใจส่วนต่าง ๆ ได้

iOS/iPhone TableView  Passing Data to Another View

เริ่มต้นด้วยการสร้าง Project บน Xcode แบบง่าย ๆ ด้วยการเลือก Single View Application

iOS/iPhone TableView  Passing Data to Another View

เลือกและไม่เลือกดังรูป

iOS/iPhone TableView  Passing Data to Another View

ตอนนี้จะมีไฟล์ Interface หรือ View เพียง 1 ชุด ซึ่งต่อไปนี้เราจะเรียกว่า View 1 โดย View นี้จะใช้แสดงข้อมูลจาก Table View








iOS/iPhone TableView  Passing Data to Another View

คลิกขวาที่ Project เลือก New File...

iOS/iPhone TableView  Passing Data to Another View

เลือก Objective-C Class

iOS/iPhone TableView  Passing Data to Another View

อย่าลืม Include xib ไฟล์ด้วย ตามรูป

iOS/iPhone TableView  Passing Data to Another View

เลือก Path ที่จัดเก็บ

iOS/iPhone TableView  Passing Data to Another View

เราจะได้ไฟล์ View ขึ้นมาอีก 1 ชุด ซึ่งต่อไปนี้เราจะเรียกมันว่า View 2 ซึ่งมีหน้าที่แสดงค่าที่ส่งมาจาก Table View ของ View 1

iOS/iPhone TableView  Passing Data to Another View

กลับมาที่ View 1 ให้ลาก Table View ไปใส่บน Interface

iOS/iPhone TableView  Passing Data to Another View

สร้าง IBOutlet และ IBAction ของ Table View ใน View 1

iOS/iPhone TableView  Passing Data to Another View

ชื่อ IBOutlet

iOS/iPhone TableView  Passing Data to Another View

ได้ IBOutlet ชื่อว่า myTable

iOS/iPhone TableView  Passing Data to Another View

ให้คลิกขวาที่ Table View ซึ่งจะมี กล่องสีดำ ๆ โชว์ดังรูป

iOS/iPhone TableView  Passing Data to Another View

ให้ลาก dataSource ไปที่ File's Owner

iOS/iPhone TableView  Passing Data to Another View

ให้ลาก delegate ไปที่ File's Owner

iOS/iPhone TableView  Passing Data to Another View

การเชื่อม dataSource และ delegate ได้ผลดังรูป

iOS/iPhone TableView  Passing Data to Another View

กลับมาที่ View 2 ให้ออกแบบหน้าจอ Interface Builder เหมือนกับในรูป และให้เชื่อม IBOutlet และ IBAction ของ Label ให้เรียบร้อยด้วย โดยตั้งชื่อ Label ว่า lblResult








ขั้นตอนถัดไปคือการเขียน Code บนภาษา Objective-C ซึ่งมีทั้งหมด 4 ไฟล์

ViewController.h
//
//  ViewController.h
//
//  Created by Weerachai on 10/28/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITableView *myTable;
}

@end


ViewController.m
//
//  ViewController.m
//
//  Created by Weerachai on 10/28/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "ViewController.h"

#import "DetailController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSMutableArray *myObject;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    myObject = [[NSMutableArray alloc] init];
    int i = 0;
    for(i = 0 ;i <= 10 ; i ++)
    {
        NSString* strItem = [NSString stringWithFormat:@"ThaiCreate.Com %d", i];
        [myObject addObject:strItem];
    }
    
}

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

- (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;
    }
    
    
    NSString *object = myObject[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *object = myObject[indexPath.row];
    
    DetailController *view2 = [[[DetailController alloc] initWithNibName:nil bundle:nil] autorelease];
    view2.detailItem = [object description];
    [self presentViewController:view2 animated:YES completion:NULL];
    
}


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

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

@end



DetailController.h
//
//  DetailController.h
//  tableViewPassData
//
//  Created by Weerachai on 10/28/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DetailController : UIViewController
{
    IBOutlet UILabel *lblResult;
}

@property (strong, nonatomic) id detailItem;

@end


DetailController.m
//
//  DetailController.m
//  tableViewPassData
//
//  Created by Weerachai on 10/28/55 BE.
//  Copyright (c) 2555 Weerachai. All rights reserved.
//

#import "DetailController.h"

@interface DetailController ()

@end

@implementation DetailController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    lblResult.text = [self.detailItem description];
}

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

- (void)dealloc {
    [lblResult release];
    [super dealloc];
}
@end




ส่วนคำอธิบาย สามารถดูแล้วเข้าใจได้โดยไม่ยาก สิ่งที่เพิ่มเข้ามาคือ method ชื่อว่า didSelectRowAtIndexPath ซึ่งเป็น Event ที่ที่เกิดขึ้นหลังจากการคลิกที่ Row Item ของ TableView

Screenshot

iOS/iPhone TableView  Passing Data to Another View

แสดงข้อมูลบน Table View

iOS/iPhone TableView  Passing Data to Another View

แสดงรายการที่ถูกส่งมาจาก View 1 ผ่านการคลิกที่ Item Rows ของ Table View

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-10-29 15:20:51 / 2017-03-26 09:56:30
  Download : Download  iOS/iPhone TableView  Passing Data to Another View (Objective-C,iPhone,iPad)
 Sponsored Links / Related

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

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

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

 
iOS/iPhone Master Detail Wizard Application (Add/Insert/Delete/Table View)
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 อัตราราคา คลิกที่นี่