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

Registered : 109,027

HOME > Mobile > [iOS/iPhone] Tutorials - สอนเขียน iPhone App ฟรี เขียน iPad App เรียน iPhone เขียนโปรแกรม iPhone > iOS/iPhone Table View(UITableView) Sections from an NSArray/NSMutableArray/NSDictionary (Objective-C,iPhone, iPad)



Clound SSD Virtual Server

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

iOS/iPhone Table View(UITableView) Sections from an NSArray/NSMutableArray/NSDictionary (Objective-C,iPhone, iPad) บทความกน่อนหน้านี้เราได้รู้วิธีการสร้าง Table View และ Section แบบ Static Cell แล้ว และบทความนี้เรามารู้วิธีการสร้างแบบ Dynamic Cell ที่ดึงและอ่านข้อมูลต่าง ๆ มาจาก Array จากนั้นก็ทำการแสดงผลตาม Section ของข้อมูลที่อยู่ในรูปแบบตามที่กำหนดขึ้น

iOS/iPhone Table View(UITableView) Sections from an NSArray

iOS/iPhone Table View(UITableView) Sections from an NSArray/NSMutableArray/NSDictionary


และเพื่อให้ได้ข้อมูลที่อยุ่ในรุปแบบเป็นแบบ Array ที่ประกอบด้วยหลาย Key และ Value เราจะต้องใช้ NSMutableArray ทำงานรวมกับ NSDictionary ซึ่งภายในแต่ล่ะ Index ของ NSMutableArray ก็จะประกอบด้วย NSDictionary ที่ประกอบด้วย Key และ Value ต่าง ๆ ตามตัวอย่าง

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];
    
    // Section Group 1
    NSArray *arrGroup1 = [NSArray arrayWithObjects:@"Item 1", @"Item 2", @"Item 3", @"Item 4", nil];
    NSDictionary *dictGroup1 = [NSDictionary dictionaryWithObject:arrGroup1 forKey:@"GroupItem"];
    
    // Section Group 2
    NSArray *arrGroup2 = [NSArray arrayWithObjects:@"Item 5", @"Item 6", @"Item 7", @"Item 8", nil];
    NSDictionary *dictGroup2 = [NSDictionary dictionaryWithObject:arrGroup2 forKey:@"GroupItem"];
    
    [listOfItems addObject:dictGroup1];
    [listOfItems addObject:dictGroup2];


เริ่มต้นการสร้าง Project บน Xcode เพื่อสร้าง Dynamic Cell แบบ Sections

iOS/iPhone Table View(UITableView) Sections from an NSArray

สร้าง Application แบบ Single View Application

iOS/iPhone Table View(UITableView) Sections from an NSArray

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

iOS/iPhone Table View(UITableView) Sections from an NSArray

หลังจากนั้นเราจะได้ View บนไฟล์ .xib ที่ยังเป็นหน้าจอขาว ๆ ว่างเปล่า ให้ลาก Table View มายังวางไว้ที่ View หลัก








iOS/iPhone Table View(UITableView) Sections from an NSArray

สร้าง IBOutlet ให้กับ Table View

iOS/iPhone Table View(UITableView) Sections from an NSArray

ได้ IBOutlet ดังรูป

iOS/iPhone Table View(UITableView) Sections from an NSArray

คลิกขวาที่ Table View จะได้หน้าจอดังรูป

iOS/iPhone Table View(UITableView) Sections from an NSArray

ทำการลาก dataSource ไปยังตำแหน่งของ File's Owner

iOS/iPhone Table View(UITableView) Sections from an NSArray

เชื่อม delegate ด้วย

iOS/iPhone Table View(UITableView) Sections from an NSArray

จะได้ผลลัพธ์ดังรูป จากนั้นจะเป้นส่วนของ Coding ด้วยภาษา Objective-C

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (retain, nonatomic) IBOutlet UITableView *myTable;

@end


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

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *listOfItems;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];
    
    // Section Group 1
    NSArray *arrGroup1 = [NSArray arrayWithObjects:@"Item 1", @"Item 2", @"Item 3", @"Item 4", nil];
    NSDictionary *dictGroup1 = [NSDictionary dictionaryWithObject:arrGroup1 forKey:@"GroupItem"];
    
    // Section Group 2
    NSArray *arrGroup2 = [NSArray arrayWithObjects:@"Item 5", @"Item 6", @"Item 7", @"Item 8", nil];
    NSDictionary *dictGroup2 = [NSDictionary dictionaryWithObject:arrGroup2 forKey:@"GroupItem"];
    
    [listOfItems addObject:dictGroup1];
    [listOfItems addObject:dictGroup2];
    
    //Set Title
    self.navigationItem.title = @"Group Option Item";
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return [listOfItems count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"GroupItem"];
    return [array count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if(section == 0)
        return @"Group 1";
    else
        return @"Group 2";
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
                                       reuseIdentifier : CellIdentifier] autorelease];
    }
    
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"GroupItem"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
    
    return cell;
}

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

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

สำหรับ Code ลองไล่ดูครับ ไม่ยากอะไรมากมาย








Screenshot

iOS/iPhone Table View(UITableView) Sections from an NSArray

ได้ Table View ที่แบางตาม Section ดังรูป

เพิ่มเติม

iOS/iPhone Table View(UITableView) Sections from an NSArray

ลองเปลี่ยนมุมมอง Style ให้เป็นแบบ Grouped

iOS/iPhone Table View(UITableView) Sections from an NSArray

ได้ Sections ที่เป็นแบบ Group สวยงาม



   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-11-07 10:36:27 / 2017-03-26 09:44:01
  Download : Download  iOS/iPhone Table View(UITableView) Sections from an NSArray/NSMutableArray/NSDictionary (Objective-C,iPhone, iPad)
 Sponsored Links / Related

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

 
iOS/iPhone TableView Passing Data to Another View (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 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 04
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 อัตราราคา คลิกที่นี่