<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Macfeteria &#187; UITableView</title>
	<atom:link href="http://www.macfeteria.com/blog/tag/uitableview/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.macfeteria.com</link>
	<description>All About Mac and iPhone Programming</description>
	<lastBuildDate>Tue, 09 Aug 2011 04:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>iPhone Programming VIIII : Table View ( Part 4) Custom Cell</title>
		<link>http://www.macfeteria.com/blog/2009/10/26/iphone-programming-viiii-table-view-part-4-custom-cell/</link>
		<comments>http://www.macfeteria.com/blog/2009/10/26/iphone-programming-viiii-table-view-part-4-custom-cell/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 09:54:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=1186</guid>
		<description><![CDATA[เห็นถามกันมาหลายคนครับ โปรแกรมหลายๆโปรแกรมใน iphone นั้นมีตารางที่มีทั้ง ปุ่ม ทั้งอะไรมากมาย ยกตัวอย่าง เช่น Setting ที่อยู่ในเครื่องอยู่แล้ว ลองกด เข้าไปดูครับจะเห็นว่า ในแต่ละ cell น้ันมีทั้ง switch on-off ทั้งตัวหนังสือ เค้าทำกันได้ยังไง ? คำตอบคือ Custom Cell ครับ วันนี้เราจะเขียน custom cell แบบง่ายๆกันครับ เพื่อเป็นแนวทางให้ไปประยุกต์ใช้กันต่อไป เอาละครับ เริ่มจากสร้างโปรเจคแบบ view base กันก่อน อย่างตัวอย่างนี้ผมตั้งชื่อว่า DemoCustomCell ครับ แล้วเราก็ เพิ่ม class ที่ชื่อว่า StudentData เข้าไปยังโปรเจค ส่วน code interface ก็ประมาณนี้ครับ @interface StudentData : NSObject &#123; NSString *studentName; NSString [...]]]></description>
			<content:encoded><![CDATA[<p>เห็นถามกันมาหลายคนครับ โปรแกรมหลายๆโปรแกรมใน iphone นั้นมีตารางที่มีทั้ง ปุ่ม ทั้งอะไรมากมาย ยกตัวอย่าง เช่น Setting ที่อยู่ในเครื่องอยู่แล้ว ลองกด เข้าไปดูครับจะเห็นว่า ในแต่ละ cell น้ันมีทั้ง switch on-off ทั้งตัวหนังสือ เค้าทำกันได้ยังไง ? คำตอบคือ Custom Cell ครับ</p>
<p>วันนี้เราจะเขียน custom cell แบบง่ายๆกันครับ เพื่อเป็นแนวทางให้ไปประยุกต์ใช้กันต่อไป เอาละครับ เริ่มจากสร้างโปรเจคแบบ view base กันก่อน อย่างตัวอย่างนี้ผมตั้งชื่อว่า DemoCustomCell ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/13.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1187 aligncenter" title="1" src="http://www.macfeteria.com/wp-content/uploads/2009/10/13-300x212.gif" alt="1" width="300" height="212" /></a></p>
<p>แล้วเราก็ เพิ่ม class ที่ชื่อว่า StudentData เข้าไปยังโปรเจค ส่วน code interface ก็ประมาณนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> StudentData <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>studentName;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>studentGrade;
	UIImage <span style="color: #002200;">*</span>studentPicture;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> studentName;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> studentGrade;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIImage <span style="color: #002200;">*</span>studentPicture;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>แล้วก็ส่วน implement ก็ไม่มีอะไรมากครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;StudentData.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> StudentData
<span style="color: #a61390;">@synthesize</span> studentName;
<span style="color: #a61390;">@synthesize</span> studentGrade;
<span style="color: #a61390;">@synthesize</span> studentPicture;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>studentName release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>studentGrade release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>studentPicture release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จาก code ข้างบน ผมได้เขียน class ที่ชื่อว่า StudentData เพื่อที่จะเก็บ ชื่อ,เกรด และ รูปภาพ ของนักเรียน<br />
และลำดับต่อไป เราก็ออกแบบหน้าตา interface ของโปรแกรม โดยเพิ่ม ตารางเข้ามา พร้อมทั้ง set delegate และ datasource ให้กับตารางให้เรียบร้อย</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/1a.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1188 aligncenter" title="1a" src="http://www.macfeteria.com/wp-content/uploads/2009/10/1a-300x222.gif" alt="1a" width="300" height="222" /></a></p>
<p>หลังจากนั้น ก็มาเขียนส่วนของ code ของ DemoCustomCell กันครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DemoCustomCellViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>studentArray;
	IBOutlet UITableView <span style="color: #002200;">*</span>studentTableView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จาก code ข้างบน เราประกาศ studentArray เพื่อเอาไว้เก็บรายชื่อนักเรียน และมี IBOutlet ที่เป็น UITableView ไว้แสดงผลครับ</p>
<p>ลำดับต่อไป เราจะเขียนส่วนของ implement ก็แบบนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;DemoCustomCellViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;StudentData.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> DemoCustomCellViewController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	studentArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	StudentData<span style="color: #002200;">*</span> student_A , <span style="color: #002200;">*</span>student_B , <span style="color: #002200;">*</span>student_C , <span style="color: #002200;">*</span>student_D;
&nbsp;
	student_A <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StudentData alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	student_B <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StudentData alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	student_C <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StudentData alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	student_D <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StudentData alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	student_A.studentName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;น้องกระแต&quot;</span><span style="color: #002200;">&#93;</span>;
	student_A.studentGrade <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A&quot;</span><span style="color: #002200;">&#93;</span>;
	student_A.studentPicture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;katae.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	student_B.studentName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;น้องปีใหม่&quot;</span><span style="color: #002200;">&#93;</span>;
	student_B.studentGrade <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;B+&quot;</span><span style="color: #002200;">&#93;</span>;
	student_B.studentPicture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mai.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	student_C.studentName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;น้องแพนเค้ก&quot;</span><span style="color: #002200;">&#93;</span>;
	student_C.studentGrade <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;C&quot;</span><span style="color: #002200;">&#93;</span>;
	student_C.studentPicture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pancake.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	student_D.studentName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;น้องเกรซ&quot;</span><span style="color: #002200;">&#93;</span>;
	student_D.studentGrade <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;B&quot;</span><span style="color: #002200;">&#93;</span>;
	student_D.studentPicture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;grace.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>studentArray addObject<span style="color: #002200;">:</span>student_A<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>studentArray addObject<span style="color: #002200;">:</span>student_B<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>studentArray addObject<span style="color: #002200;">:</span>student_C<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>studentArray addObject<span style="color: #002200;">:</span>student_D<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>student_A release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>student_B release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>student_C release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>student_D release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidUnload
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>studentArray release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// -----------------------------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">//			TableView Delegate Implement</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------------------------------------------</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>studentArray count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>table
		 cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;
<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>sampleIndentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Identifier&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell;
        cell<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>studentTableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>
			sampleIndentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
		reuseIdentifier<span style="color: #002200;">:</span>sampleIndentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
&nbsp;
	StudentData <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>studentArray objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	cell.textLabel.text <span style="color: #002200;">=</span> temp.studentName;
&nbsp;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>จากข้างบน ผมได้เพิ่มนักเรียนเข้าไปยัง StudentArray รวมทั้งหมด 4 คนครับ และในส่วนของ table delegate นั้นก็ ได้กำหนดให้ cell นั้นแสดง ชื่อของนักเรียน และเมื่อเราทำการ compile ก็จะได้โปรแกรมแบบนี้ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/31.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1193 aligncenter" title="3" src="http://www.macfeteria.com/wp-content/uploads/2009/10/31-156x300.gif" alt="3" width="156" height="300" /></a></p>
<p>ไม่ยากใช่ไหมครับ ต่อไปเราจะเขียน Custom Cell ครับ</p>
<h2>Custom Cell</h2>
<p>จากโปรแกรมข้างบน เราจะเห็นว่า ข้อจำกัดของโปรแกรมคือ ในแต่ละ cell นั้นแสดงได้แค่ ตัวหนังสือ เท่านั้นและได้เพียงแค่หนึ่งแถว ถ้าหากเราอยากจะเพิ่ม ให้ cell ของเราแสดงข้อมูลได้มากกว่านี้ เราจะต้องทำ custom cell ครับ เอาละครับ เริ่มกันเลย ก่อนอื่นเราต้องเพิ่ม xib เข้ามายังโปรเจค ครับ ดังรูป ก็ click ขวาเลือก new file แล้วก็เลือกไปที่ Empty XIB ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/21.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1194 aligncenter" title="2" src="http://www.macfeteria.com/wp-content/uploads/2009/10/21-300x214.gif" alt="2" width="300" height="214" /></a></p>
<p>แล้วก็ตั้งชื่อครับ ในตัวอย่างนี้ผมตั้งชื่อ StudentCustomCell.xib ครับ เมื่อเปิด File XIB ขึ้นมาก็จะเห็น เพียงแค่ File&#8217;s Owner กับ First Responder หลังจากนั้นให้ลาก UITableViewCell เข้ามายัง Document ของเรา ดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/4.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1196 aligncenter" title="4" src="http://www.macfeteria.com/wp-content/uploads/2009/10/4-300x201.gif" alt="4" width="300" height="201" /></a></p>
<p>เอาละครับ ก่อนที่เราจะลงมือ ออกแบบหน้าตา Custom Cell ของเรา เราต้องเขียน Class ขึ้นมาก่อน และสำหรับ Custom Cell ที่ผมจะออกแบบนี้ ผมอยากให้ มี</p>
<ol>
<li>ชื่อของนักเรียน</li>
<li>เกรดของนักเรียน</li>
<li>รูปถ่ายนักเรียน</li>
</ol>
<p>เอาละครับ ต่อไปเราก็ลงมือเขียน StudentCustomCell Class ของเราเลย โดยการ Add New File อย่างที่เคยทำ นั่นเหละครับ แต่ให้เลือก SubClass  of ให้เป็น UITableViewCell แบบในรูปครับ แล้วก็ตั้งชื่อว่า StudentCustomCell</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/5.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1197 aligncenter" title="5" src="http://www.macfeteria.com/wp-content/uploads/2009/10/5-300x214.gif" alt="5" width="300" height="214" /></a></p>
<p>แล้ว code ของ StudentCustomCell.h ก็ได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> StudentCustomCell <span style="color: #002200;">:</span> UITableViewCell
<span style="color: #002200;">&#123;</span>
	IBOutlet UILabel<span style="color: #002200;">*</span> name;
	IBOutlet UILabel<span style="color: #002200;">*</span> grade;
	IBOutlet UIImageView<span style="color: #002200;">*</span> picture;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span>  UILabel<span style="color: #002200;">*</span> name;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span>  UILabel<span style="color: #002200;">*</span> grade;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span>  UIImageView<span style="color: #002200;">*</span> picture;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ต่อไปก็เขียน implement ได้แบบนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;StudentCustomCell.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> StudentCustomCell
&nbsp;
<span style="color: #a61390;">@synthesize</span> name;
<span style="color: #a61390;">@synthesize</span> grade;
<span style="color: #a61390;">@synthesize</span> picture;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStyle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableViewCellStyle<span style="color: #002200;">&#41;</span>style
        reuseIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>reuseIdentifier <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithStyle<span style="color: #002200;">:</span>style reuseIdentifier<span style="color: #002200;">:</span>reuseIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Initialization code</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelected<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>selected animated<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>super setSelected<span style="color: #002200;">:</span>selected animated<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>name release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>grade release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>picture release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ครับเพียงเท่านี้ Class ของเราก็เกือบจะเสร็จละครับ ต่อไปเราก็กลับมายัง interface builder เพื่อที่จะออกแบบหน้าตา interface ของเราและ set link ต่างๆเข้ากับ class ที่เราได้เขียนขึ้นมา</p>
<h2>Design Custom Cell</h2>
<p>โดยลำดับแรกที่ต้องทำก็คือ เปิด inspector ขึ้นมาแล้วก็ set ให้ UITableViewCell ของเราเป็น Student Custom Cell ดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/6.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1198 aligncenter" title="6" src="http://www.macfeteria.com/wp-content/uploads/2009/10/6-236x300.gif" alt="6" width="236" height="300" /></a></p>
<p>หลังจากได้เปลี่ยนให้ UITableViewCell เป็น StudentCustomCell แล้ว ก็ออกแบบหน้าตาของโปรแกรม พร้อมกับเชื่อม Outlet ต่างๆให้เรียบร้อยดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/7.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1199 aligncenter" title="7" src="http://www.macfeteria.com/wp-content/uploads/2009/10/7-279x300.gif" alt="7" width="279" height="300" /></a></p>
<h2>Coding</h2>
<p>ลำดับต่อไปเราก็จะแก้ไข code กันครับ ก่อนที่จะแก้ไขหรือเปลี่ยนแปลงอะไรใน code เราต้องเข้าใจก่อนครับว่า ที่เราได้ทำไปทั้งหมด เมื่อกี้คืออะไร สิ่งที่เราได้ทำไปทั้งหมดนั้นคือ</p>
<ul>
<li>ประกาศ Custom Cell</li>
<li>ออกแบบ Custom Cell</li>
</ul>
<p>สุดท้ายที่จะทำก็คือ เอามาใช้ครับ วิธีการที่เราจะเอา Custom Cell ของเรามาใช้ ก็ไม่ยากครับ ถ้าสังเกตดีๆ จะเห็น code ส่วนของ table delegate  ตรงนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>table
		 cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;
<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>sampleIndentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Identifier&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell;
	cell<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>studentTableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>
						   sampleIndentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
			reuseIdentifier<span style="color: #002200;">:</span>sampleIndentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
&nbsp;
	StudentData <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>studentArray objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	cell.textLabel.text <span style="color: #002200;">=</span> temp.studentName;
&nbsp;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จากตรง code ข้างบน จะเห็นว่า เราได้ประกาศ cell ขึ้นมาให้เป็น UITableViewCell ใช่ไหมครับ ? แล้วเราก็ทำการ alloc แล้วก็ init มัน เอาละ งั้นถ้าหากว่าเราต้องการให้มันเป็น custom cell ทำยังไงละครับ คำตอบก็ไม่ยากเลย ก็คือเราก็ประกาศให้ cell ของเราเป็น custom cell ซะก็เรียบร้อย งั้นเรามาดู code กันเลย</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>table
		 cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;
<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>sampleIndentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Identifier&quot;</span>;
	StudentCustomCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>StudentCustomCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>studentTableView
		dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>sampleIndentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>topLevelObjects;
	topLevelObjets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span>
	loadNibNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;StudentCustomCell&quot;</span> owner<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> currentObject <span style="color: #a61390;">in</span> topLevelObjects<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>currentObject isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>StudentCustomCell class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>StudentCustomCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>currentObject;
			<span style="color: #a61390;">break</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
&nbsp;
	StudentData <span style="color: #002200;">*</span>temp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>studentArray objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	cell.name.text <span style="color: #002200;">=</span> temp.studentName;
	cell.grade.text <span style="color: #002200;">=</span> temp.studentGrade;
	cell.picture.image <span style="color: #002200;">=</span> temp.studentPicture;
&nbsp;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>อธิบายจาก ข้างบนนะครับ เราได้ประกาศ cell ให้เป็น StudentCustomCell โดยที่เราได้โหลดจาก StudentCustomCell.XIB ( ตรงส่วนของ for loop เป็นแค่ส่วนที่ใช้หาว่า object ตัวไหนใน xib ที่เป็น StudentCustomCell เท่านั้นเอง )  หลังจากนั้นเราก็ ได้ใส่รูป ใส่ชื่อ ใส่เกรด ให้กับ cell ก็เป็นอันจบ เมื่อ compile &amp; run ก็จะเห็นผลลัพธ์ดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/10/result1.gif" rel="lightbox[1186]"><img class="size-medium wp-image-1202 aligncenter" title="result1" src="http://www.macfeteria.com/wp-content/uploads/2009/10/result1-156x300.gif" alt="result1" width="156" height="300" /></a></p>
<p>จะสังเกตเห็นว่า มันไม่พอดีกับ ตารางของเรา เราจำเป้นต้องเปลี่ยนความสูงของ cell ที่ใช้ โดยเพิ่ม</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView
	heightForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">120</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: center;">เพียงเท่านี้ก็จะได้ผลลัพธ์เหมือนในรูปครับ<br />
<a href="http://www.macfeteria.com/wp-content/uploads/2009/10/result_done.png" rel="lightbox[1186]"><img class="size-medium wp-image-1203 aligncenter" title="result_done" src="http://www.macfeteria.com/wp-content/uploads/2009/10/result_done-156x300.png" alt="result_done" width="156" height="300" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">ลอง Download Code ไป Compile ดูได้ครับ <a href="http://www.macfeteria.com/wp-content/uploads/2009/10/DemoCustomCell.zip">DemoCustomCell</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/cocoa+touch' rel='tag' target='_self'>cocoa touch</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableView' rel='tag' target='_self'>UITableView</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableViewCell' rel='tag' target='_self'>UITableViewCell</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2009/10/26/iphone-programming-viiii-table-view-part-4-custom-cell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iPhone Programming VII : Table View ( Part 3)</title>
		<link>http://www.macfeteria.com/blog/2009/07/16/iphone-programming-vii-table-view-part-3/</link>
		<comments>http://www.macfeteria.com/blog/2009/07/16/iphone-programming-vii-table-view-part-3/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 08:56:49 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[UITableView]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=976</guid>
		<description><![CDATA[วันนี้ก็ยังต่อเนื่องเกี่ยวกับเรื่องของ table view เหมือนเดิมนะครับ สืบเนื่องจากครั้งที่แล้วที่ผม ได้เขียน tutorial ในแบบลักษณะการเขียนย่อลง เอาแต่ส่วนที่สำคัญทำให้ผู้อ่านหลายๆคน บอกว่าน่าจะเอาแบบเดิม ( คือเริ่มตั้งแต่ สร้าง project ) เพราะ ยังประสบการณ์น้อย อ่านแล้วไม่เข้าใจ โอเค ครับ งั้นผมจะ พยายาม เขียน ในทุกๆขั้นตอนเลยละกัน ( แต่ในอนาคจะ ค่อยๆ ปรับลดลงนะครับ ) อย่างที่บอกไปว่าวันนี้ยังอยู่ในเรื่องของ table เหมือนเดิม วันนี้จะมาต่อในเรื่องของ Section ครับ โดยปกติแล้ว Table View น้ันจะประกอบไปด้วย cell และ cell เหล่านี้สามารถแยกเป็นหมวดๆได้ เรียนกว่า section ดูรูปประกอบ จะเข้าใจ จากรูปจะเห็นชื่อที่ขึ้นต้นด้วย ตัวอักษรเดียวกันนั้นจะถูกจับรวมกันเป็นกลุ่ม ( section ) งั้นเรามาเริ่มเลยดีกว่าครับ โปรแกรมที่เราจะเขียนกันในวันนี้ก็ไม่มีอะไรมากครับ คือเราจะเขียน [...]]]></description>
			<content:encoded><![CDATA[<p>วันนี้ก็ยังต่อเนื่องเกี่ยวกับเรื่องของ table view เหมือนเดิมนะครับ สืบเนื่องจากครั้งที่แล้วที่ผม ได้เขียน tutorial ในแบบลักษณะการเขียนย่อลง เอาแต่ส่วนที่สำคัญทำให้ผู้อ่านหลายๆคน บอกว่าน่าจะเอาแบบเดิม ( คือเริ่มตั้งแต่ สร้าง project ) เพราะ ยังประสบการณ์น้อย อ่านแล้วไม่เข้าใจ โอเค ครับ งั้นผมจะ พยายาม เขียน ในทุกๆขั้นตอนเลยละกัน ( แต่ในอนาคจะ ค่อยๆ ปรับลดลงนะครับ )</p>
<p>อย่างที่บอกไปว่าวันนี้ยังอยู่ในเรื่องของ table เหมือนเดิม วันนี้จะมาต่อในเรื่องของ Section ครับ<br />
โดยปกติแล้ว Table View น้ันจะประกอบไปด้วย cell และ cell เหล่านี้สามารถแยกเป็นหมวดๆได้ เรียนกว่า section ดูรูปประกอบ จะเข้าใจ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_section.png" rel="lightbox[976]"><img class="size-medium wp-image-977 aligncenter" title="table_section" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_section-156x300.png" alt="table_section" width="156" height="300" /></a></p>
<p style="text-align: left;">
<p>จากรูปจะเห็นชื่อที่ขึ้นต้นด้วย ตัวอักษรเดียวกันนั้นจะถูกจับรวมกันเป็นกลุ่ม ( section ) งั้นเรามาเริ่มเลยดีกว่าครับ</p>
<p>โปรแกรมที่เราจะเขียนกันในวันนี้ก็ไม่มีอะไรมากครับ คือเราจะเขียน โปรแกรม ไว้เก็บรายชื่อของสัตว์ต่างๆ แล้วก็แยกตามหมวดหมู่เท่านั้นเอง แต่ก่อนที่จะเริ่มดูภาพประกอบกันก่อนครับว่า โครงสร้างของโปรแกรม เราเป็นแบบไหน จะได้เข้าใจกันก่อนสักนิดว่า เรากำลังจะทำอะไร</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/chart.png" rel="lightbox[976]"><img class="size-medium wp-image-980 aligncenter" title="chart" src="http://www.macfeteria.com/wp-content/uploads/2009/07/chart-300x125.png" alt="chart" width="300" height="125" /></a></p>
<p style="text-align: left;">ตามแผนภาพ ผมจะแบ่งหมวดหมู่ของสัตว์ออกเป็น 3 ประเภทคือ Mammal , Bird , Fish แล้วแต่ละประเภท นั้นก็จะมีรายชื่อของ สัตว์ต่างๆ การออกแบบ คลาสต่างๆ ก็จะเหมือนดังแผนภาพ ครับ นั่นก็คือ ผมมี class ที่ชื่อ Animal เป็น ทั้ง datasource , delegate ( NSTableView ) เมื่อดูภาพประกอบคร่าวๆ เรียบร้อยแล้ว ต่อไปก็เริ่มโปรเจคกันเลย</p>
<p style="text-align: left;">Start New Project</p>
<p style="text-align: left;">เราก็เลือก view based application ครับ แล้วหลังจากนั้นก็ตั้งชื่ออะไรก็ได้ ในตัวอย่างนี้ผมจะใช้ชื่อว่า AnimalDemo เสร็จแล้วก็จะได้โปรเจคออกมาดังรูปแบบนี้ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/new_project.png" rel="lightbox[976]"><img class="size-medium wp-image-981 aligncenter" title="new_project" src="http://www.macfeteria.com/wp-content/uploads/2009/07/new_project-300x212.png" alt="new_project" width="300" height="212" /></a></p>
<p style="text-align: left;">เมื่อเรียบร้อยแล้ว ลำดับต่อไป เราก็จะออกแบบหน้าตาของโปรแกรมเราก่อน ก็ทำการเปิด AnimalDemoViewController.xib ขึ้นมา แล้วก็ลาก Table View เข้ามาดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_demo.png" rel="lightbox[976]"><img class="size-medium wp-image-982 aligncenter" title="table_demo" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_demo-191x300.png" alt="table_demo" width="191" height="300" /></a></p>
<p style="text-align: left;">ก็เป็นอันเสร็จ การออกแบบหน้าตาของโปรแกรม  หลังจากนี้ ลำดับต่อไปก็คือ เขียน class ที่เป็น datasource และ delegate นั่นเอง<br />
เราจะทำการเพิ่ม class เข้ามายัง project ของเราโดยการ เลือกไปที่เมนู File &gt; New File&#8230; แล้วก็เลือก ชนิดของ File ดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/new_class.png" rel="lightbox[976]"><img class="size-medium wp-image-983 aligncenter" title="new_class" src="http://www.macfeteria.com/wp-content/uploads/2009/07/new_class-300x206.png" alt="new_class" width="300" height="206" /></a></p>
<p style="text-align: left;">หลังจากนั้นก็ตั้งชื่อว่า Animal ก็เป็นอันเสร็จเรียบร้อย ต่อไปเราก็จะมาเขียน code กัน</p>
<h2 style="text-align: left;">Implement Delegate &amp; Data Source</h2>
<p style="text-align: left;">อย่างที่เห็นในแผนภาพ ไปแล้ว ว่าเรามี class ที่ชื่อ Animal ที่ประกอบไปด้วย Array ที่เก็บรายชื่อของ สัตว์ต่างๆ เวลาเขียน code ออกมาก็จะได้ประมาณนี้ครับ</p>
<p style="text-align: left;">

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Animal.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Animal
<span style="color: #a61390;">@synthesize</span> m_animal;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	m_animal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> mammal<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Dog&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cat&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Rabbit&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tiger&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span> ;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> bird <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Eagle&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hawk&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Parrot&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> fish <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Shark&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Barrachuda&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Gold Fish&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>mammal forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mammal&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>bird forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bird&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>fish forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fish&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// Total Section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> table
<span style="color: #002200;">&#123;</span>
	NSInteger totalSection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal count<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> totalSection;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Number of row in each section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
			 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> keyValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>key objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	NSInteger rowCount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self.m_animal objectForKey<span style="color: #002200;">:</span>keyValue<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> rowCount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Name of each section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
			 titleForHeaderInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> allKeys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>allKeys objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> name;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UITableViewCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
		cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> indexPath
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>sampleIndentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Identifier&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>
						sampleIndentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
		reuseIdentifier<span style="color: #002200;">:</span>sampleIndentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
	NSUInteger section <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath section<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> keyValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>key objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> animalList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_animal objectForKey<span style="color: #002200;">:</span>keyValue<span style="color: #002200;">&#93;</span>;
	cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>animalList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_animal release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h2>Explain</h2>
<p>ดู code แล้วอาจจะค่อนข้างจะยาวนิดหนึ่งนะครับ ค่อยๆทำความเข้าใจกันในแต่ละส่วนก่อนดีกว่า</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	m_animal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> mammal<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Dog&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cat&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Rabbit&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tiger&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span> ;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> bird <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Eagle&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hawk&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Parrot&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> fish <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
		 <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Shark&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Barrachuda&quot;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Gold Fish&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>mammal forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mammal&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>bird forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bird&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>m_animal setObject<span style="color: #002200;">:</span>fish forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fish&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ฟังก์ชั่นนี้ ไม่ได้มีอะไรมากนอกจาก ประกาศ array ที่ประกอบไปด้วยรายชื่อของสัตว์ต่างๆ และนำมันไปใส่ไว้ใน dictionary ( m_animal ) จริงๆแล้วเราจะใช้ array ทั้งหมดก็ได้นะครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Total Section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> table
<span style="color: #002200;">&#123;</span>
	NSInteger totalSection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal count<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> totalSection;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Number of row in each section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
			 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> keyValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>key objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	NSInteger rowCount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self.m_animal objectForKey<span style="color: #002200;">:</span>keyValue<span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> rowCount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Name of each section</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
			 titleForHeaderInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> allKeys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>allKeys objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> name;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>สองฟังก์ชั่น ข้างบน นั้น เป็นการบอกว่า ตารางนี้ประกอบไปด้วย section ทั้งหมดเท่าไหร่ และแต่ละ section นั้นมีแถวจำนวนเท่าไหร่<br />
ซึ่งจำนวน section ของเราทั้งหมดนั้นมี จะมีเท่ากับ จำนวน array ที่ได้เก็บไว้ใน m_animal นั่นก็คือ 3 และจำนวน แถวของแต่ละ section นั้นก็หามาจาก จำนวนชื่อของสัตว์ในแต่ละ array ที่ได้เก็บไว้นั่นเอง<br />
และฟังก์ชั่นสุดท้าย ก็คือ ชื่อของแต่ละ section ในตัวอย่างผมได้ให้ ชื่อของแต่ละ section น้ันเป็นชื่อของ key ที่ผมได้ใส่ไปใน dictionary ตอนสร้างครั้งแรกนั่นเอง</p>
<p>หมายเหตุ ใน Dictionary เราไม่สามารถจะขอข้อมูลที่เราได้ใส่ไปถ้าหากไม่มี key ดังนั้นแล้ว ผมจึงใช้ ** [NSDictionary allKey] เพื่อที่จะได้ key ทั้งหมดกลับมาซึ่งจะเป็น array ** หลังจากนั้นแล้วค่อยไปไล่หาค่าต่างๆตามลำดับ index ใน array อีกที</p>
<p>ส่วนสุดท้ายก็คือ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UITableViewCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
		cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> indexPath
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>sampleIndentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Identifier&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>
						sampleIndentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
		reuseIdentifier<span style="color: #002200;">:</span>sampleIndentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
	NSUInteger section <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath section<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> key <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.m_animal allKeys<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> keyValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>key objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> animalList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_animal objectForKey<span style="color: #002200;">:</span>keyValue<span style="color: #002200;">&#93;</span>;
	cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>animalList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>เราได้ implement delegate เพื่อที่ตารางจะได้รู้ว่าควรนำอะไรไปแสดง ในแต่ละ section , row<br />
จาก code ข้างบน อธิบายคร่าวก็คือ เมื่อเรา ได้ index path มาแล้วเราหากลุ่มจาก section เมื่อเรารู้กลุ่มแล้ว หลังจากนั้นก็ไปหา ชื่อของสัตว์ตามลำดับของแถว ที่ควรจะแสดง</p>
<p>ก็เป็นอันจบในส่วนของ Animal Class</p>
<p>แต่ยังเหลือในส่วนของ View Controller ที่เรายังไม่ได้เขียนเพิ่มครับ เราต้อง เพิ่ม Animal เข้าไปยัง View Controller ของเรา ก็เปิด File ที่ชื่อ AnimalDemoViewController.h ขึ้นมาครับ<br />
หลังจากนั้นก็เพิ่ม code เข้าไปดังนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Animal.h&quot;</span>
<span style="color: #a61390;">@interface</span> AnimalDemoViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
	IBOutlet Animal <span style="color: #002200;">*</span>m_animalDataSource;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> Animal<span style="color: #002200;">*</span> m_animalDataSource;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>แล้วส่วน implement ก็จะเป็นประมาณนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;AnimalDemoViewController.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> AnimalDemoViewController
<span style="color: #a61390;">@synthesize</span> m_animalDataSource;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>didReceiveMemoryWarning <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Releases the view if it doesn't have a superview.</span>
    <span style="color: #002200;">&#91;</span>super didReceiveMemoryWarning<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_animalDataSource release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>ก็เป็นอันเสร็จ อธิบายกันสักนิด ที่เราได้เพิ่มเข้าไปก็คือ Animal ( m_animalDataSource ) แต่เราได้ประกาศให้เป็น Outlet เพื่อที่ว่าจะได้นำไป link กับตารางที่อยู่ interface builder ครับ</p>
<p>Link Object</p>
<p>กลับไปยัง interface builder ครับ เรายังเหลือสิ่งที่ต้องทำอีก อย่างสองอย่าง นั่นก็คือ สร้าง instance ของ Animal ก็เริ่มจาก เปิด AnimalDemoViewController.xib ขึ้นมา หลังจากนั้นก็เพิ่ม Object เข้าไปดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/animal_object.png" rel="lightbox[976]"><img class="size-medium wp-image-1005 aligncenter" title="animal_object" src="http://www.macfeteria.com/wp-content/uploads/2009/07/animal_object-272x300.png" alt="animal_object" width="272" height="300" /></a></p>
<p>จากรูป เมื่อ ลาก Object เข้ามาแล้ว เราก็เปิด หน้าต่าง inspector ขึ้นมาครับ แล้วตรง indentity จะเห็น Class เราก็เปลี่ยนให้เป็น Animal ก็เรียบร้อย เพียงเท่านี้เราก็มี Animal Instance เรียบร้อย  หลังจากนั้นก็เชื่อมมันเข้ากับ m_animalDataSource ของเรา ดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/link_instance.png" rel="lightbox[976]"><img class="size-medium wp-image-1006 aligncenter" title="link_instance" src="http://www.macfeteria.com/wp-content/uploads/2009/07/link_instance-300x239.png" alt="link_instance" width="300" height="239" /></a></p>
<p>และลำดับสุดท้ายครับ เราต้อง set datasource ให้กับตารางของเรา เราก็ click ขวาไปยังตาราง แล้วก็ลาก เชื่อมเข้ากับ Animal Instance ดังรูปครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/link_data-source.png" rel="lightbox[976]"><img class="size-medium wp-image-1007 aligncenter" title="link_data-source" src="http://www.macfeteria.com/wp-content/uploads/2009/07/link_data-source-300x222.png" alt="link_data-source" width="300" height="222" /></a></p>
<p>ก็เป็นอันเสร็จทุกๆอย่างครับ Compile แล้วก็ Run จะเห็นโปรแกรมมีหน้าตา แบบนี้ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/animal_done.png" rel="lightbox[976]"><img class="size-medium wp-image-1008 aligncenter" title="animal_done" src="http://www.macfeteria.com/wp-content/uploads/2009/07/animal_done-156x300.png" alt="animal_done" width="156" height="300" /></a></p>
<p>ครั้งหน้าเดี๋ยวมาต่อกัน ในเรื่องของ ตารางต่อนะครับ<br />
ตัวอย่าง Source Code ก็กด <a title="Animal Source Code" href="http://www.macfeteria.com/wp-content/uploads/2009/07/AnimalDemo.zip">Download</a> ได้เลยครับ</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/basic' rel='tag' target='_self'>basic</a>, <a class='technorati-link' href='http://technorati.com/tag/cocoa+touch' rel='tag' target='_self'>cocoa touch</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableView' rel='tag' target='_self'>UITableView</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2009/07/16/iphone-programming-vii-table-view-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Programming VI : Table View ( Path 2 )</title>
		<link>http://www.macfeteria.com/blog/2009/07/14/iphone-programming-iv-table-view-path-2/</link>
		<comments>http://www.macfeteria.com/blog/2009/07/14/iphone-programming-iv-table-view-path-2/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 07:36:32 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[UITableView]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=953</guid>
		<description><![CDATA[วันนี้มาต่อเรื่องของ table view กันหน่อยดีกว่า Adding Image to Cell เมื่อครั้งก่อน เราได้ใช้งาน table view ที่ไว้แสดงผลแต่ตัวหนังสือ จริงๆแล้วเราสามารถเพิ่ม รูปภาพเข้าไปยัง cell ได้ด้วย วิธีการก็ไม่ได้ยากอะไรเลยครับ เราก็เพียงแต่ สร้าง image ขึ้นมาแล้วก็ set ให้ cell เท่านั้นเองดูตัวอย่างอาจจะเข้าใจง่ายกว่า จากการ implement ของ method - (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath; ในครั้งก่อน เราจะทำการเพิ่ม image เข้าไป โดยเพิ่ม code เข้าไปดังนี้ครับ - &#40;UITableViewCell *&#41;tableView:&#40;UITableView *&#41;tableView cellForRowAtIndexPath:&#40;NSIndexPath *&#41;indexPath &#123; static NSString *identifier [...]]]></description>
			<content:encoded><![CDATA[<p>วันนี้มาต่อเรื่องของ table view กันหน่อยดีกว่า</p>
<h2>Adding Image to Cell</h2>
<p>เมื่อครั้งก่อน เราได้ใช้งาน table view ที่ไว้แสดงผลแต่ตัวหนังสือ จริงๆแล้วเราสามารถเพิ่ม รูปภาพเข้าไปยัง cell ได้ด้วย วิธีการก็ไม่ได้ยากอะไรเลยครับ เราก็เพียงแต่ สร้าง image ขึ้นมาแล้วก็ set ให้ cell เท่านั้นเองดูตัวอย่างอาจจะเข้าใจง่ายกว่า จากการ implement ของ method<br />
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath;<br />
ในครั้งก่อน เราจะทำการเพิ่ม image เข้าไป โดยเพิ่ม code เข้าไปดังนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView
   cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>identifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Macfeteria Cell&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell;
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
			reuseIdentifier<span style="color: #002200;">:</span> identifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// New Code !!!!</span>
	UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;face.png&quot;</span><span style="color: #002200;">&#93;</span>;
	cell.imageView.image <span style="color: #002200;">=</span> image;
	<span style="color: #11740a; font-style: italic;">//-------------</span>
&nbsp;
	cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_studentList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>จะเห็นว่า เราได้เขียน code ขึ้นมาเพิ่มเพียง 2 บรรทัดเองครับ คือ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;face.png&quot;</span><span style="color: #002200;">&#93;</span>;
	cell.imageView.image <span style="color: #002200;">=</span> image;</pre></div></div>

<p>เมื่อ run program หน้าตาก็จะเป็นประมาณแบบนี้</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_image.gif" rel="lightbox[953]"><img class="size-medium wp-image-958 aligncenter" title="table_with_image" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_image-156x300.gif" alt="table_with_image" width="156" height="300" /></a></p>
<h2>Row Selection</h2>
<p>วิธีการจัดการเกี่ยวกับการเลือกแถวในตาราง เราอาจจะใช้ method ที่ใช้ถามว่าแถวไหนถูกเลือกอยู่ โดยเรียกใช้ method ที่ชื่อว่า indexPathForSelectedRow ก็ได้ครับ ตารางก็จะบอกเรามาว่าแถวไหนถูกเลือกอยู่ ดังตัวอย่าง</p>
<p>สมมติว่า ผมเขียน IBAction สำหรับ Button ไว้ชื่อว่า getSelectRow นะครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> getSelectRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
   <span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_tableView indexPathForSelectedRow<span style="color: #002200;">&#93;</span>;
   <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>selectRow;
   selectRow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Row: %d&quot;</span>,<span style="color: #002200;">&#91;</span>path row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
   m_text.text <span style="color: #002200;">=</span> selectRow;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ผลลัพธ์ก็จะมีหน้าตาประมาณนี้</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_row_selected.gif" rel="lightbox[953]"><img class="size-medium wp-image-959 aligncenter" title="table_with_row_selected" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_row_selected-156x300.gif" alt="table_with_row_selected" width="156" height="300" /></a></p>
<p>เมื่อกด ไปที่ตาราง แล้วก็กด ปุ่มมันก็จะบอกว่า เราเลือกที่แถวไหน ทำได้ไม่ยากเลยครับ<br />
และ table view นั้นมี delegate ที่เกี่ยวกับการเลือกแถวที่น่าสนใจ ได้แก่ delegate แรกจะเป็นการเขียนเหตุการณ์ที่เกิดขึ้นก่อนการเลือกแถว ที่ชื่อว่า<br />
-(NSInteger) tableView: (UITableView*) tableView willSelectRowAtIndexPath: (NSIndexPath*) indexPath<br />
ตัวอย่าง code จะเป็นประมาณนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
willSelectRowAtIndexPath<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> indexPath
<span style="color: #002200;">&#123;</span>
   NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
   <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> row <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
&nbsp;
   <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>selectRow;
   selectRow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Row: %d&quot;</span>,<span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
   m_text.text <span style="color: #002200;">=</span> selectRow;
&nbsp;
   <span style="color: #a61390;">return</span> indexPath;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>เราอาจจะเอา button ออก แต่เมื่อกดไปที่ตาราง มันก็จะบอกแถวมาเรียบร้อยเลย เพราะว่าเมื่อตารางถูกเลือก มันจะมาเรียก delegate โดยอัตโนมัติ เหมือนกับรูปข้างล่าง</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_row_delegate.gif" rel="lightbox[953]"><img class="size-medium wp-image-960 aligncenter" title="table_with_row_delegate" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_row_delegate-156x300.gif" alt="table_with_row_delegate" width="156" height="300" /></a></p>
<p style="text-align: left;">และมีอีก delegate ที่น่าสนใจคือ เหตุการณ์หลังจากกดไปที่แถวของตาราง นั่นก็คือ<br />
-(void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath<br />
ตัวอย่าง code การใช้งานก็จะเช่นว่า เราต้องการให้มันแสดง alert ขึ้นมาหลังจากกดไปที่แถวของตารางนะครับ ก็จะเขียนได้ประมาณนี้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
didSelectRowAtIndexPath<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> indexPath
<span style="color: #002200;">&#123;</span>
   NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
   <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>rowData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_nameList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
&nbsp;
   <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>msg;
   msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Animal: %@&quot;</span>,rowData<span style="color: #002200;">&#93;</span>;
   UIAlertView <span style="color: #002200;">*</span>alert;
   alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You select&quot;</span>
              message<span style="color: #002200;">:</span>msg delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
	      cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok !!&quot;</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
   <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>หน้าตาหลังจาก run program ก็จะเป็นแบบนี้ครับ</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_delegate_alert.gif" rel="lightbox[953]"><img class="size-medium wp-image-961 aligncenter" title="table_with_delegate_alert" src="http://www.macfeteria.com/wp-content/uploads/2009/07/table_with_delegate_alert-156x300.gif" alt="table_with_delegate_alert" width="156" height="300" /></a></p>
<p style="text-align: left;">วันนี้ก็จบเพียงเท่านี้ครับ ครั้งหน้า เดี๋ยวมาต่อด้วย custom cell</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/basic' rel='tag' target='_self'>basic</a>, <a class='technorati-link' href='http://technorati.com/tag/cocoa+touch' rel='tag' target='_self'>cocoa touch</a>, <a class='technorati-link' href='http://technorati.com/tag/iphone' rel='tag' target='_self'>iphone</a>, <a class='technorati-link' href='http://technorati.com/tag/objective-c' rel='tag' target='_self'>objective-c</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableView' rel='tag' target='_self'>UITableView</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2009/07/14/iphone-programming-iv-table-view-path-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>iPhone Programming IV : Table View ( Path 1 )</title>
		<link>http://www.macfeteria.com/blog/2009/04/09/iphone-programming-iv-table-view-path-1/</link>
		<comments>http://www.macfeteria.com/blog/2009/04/09/iphone-programming-iv-table-view-path-1/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 06:11:50 +0000</pubDate>
		<dc:creator>[Ter]</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>

		<guid isPermaLink="false">http://www.macfeteria.com/?p=870</guid>
		<description><![CDATA[Table View นั้นเป็น Control ที่ค่อนข้างจะใช้งานเยอะมากๆ และทรงพลังมาก ถ้าเราดูโปรแกรมหลายๆโปรแกรมจะพบว่า ได้มีการประยุกต์ table view ใช้งานต่างๆมากมาย เช่นเปิดโปรแกรม Photos ก็เห็นแล้วว่ามี table view  หรือ  Contacts เป็นต้น ( ดูรูปประกอบ จะเห็นว่าทั้งหมดนี้ ล้วนแต่ใช้ table view ทั้งนั้น แต่ต่างกันในส่วนว่าจะแสดงอะไร มีรูป หรือมี control อื่นๆด้วยหรือเปล่า ) และวันนี้เราจะมาลองใช้งาน table view กันครับ Table View ใน Cocoa touch นั้นต่างกับใน Cocoa ปกติอยู่พอสมควร แต่โครงสร้างหลักๆ และหลักการ รวมถึงวิธีการใช้งานก็เหมือนเดิม ฉนั้นแล้วคนที่เคยใช้ NSTableView มาก่อน ก็หวานหมูเลย จริงๆแล้วข้อแตกต่างที่เห็นได้ชัดๆเลยก็คือ Table View ใน [...]]]></description>
			<content:encoded><![CDATA[<p>Table View นั้นเป็น Control ที่ค่อนข้างจะใช้งานเยอะมากๆ และทรงพลังมาก ถ้าเราดูโปรแกรมหลายๆโปรแกรมจะพบว่า ได้มีการประยุกต์ table view ใช้งานต่างๆมากมาย เช่นเปิดโปรแกรม Photos ก็เห็นแล้วว่ามี table view  หรือ  Contacts เป็นต้น ( ดูรูปประกอบ จะเห็นว่าทั้งหมดนี้ ล้วนแต่ใช้ table view ทั้งนั้น แต่ต่างกันในส่วนว่าจะแสดงอะไร มีรูป หรือมี control อื่นๆด้วยหรือเปล่า )</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/table_example.png" rel="lightbox[870]"><img class="size-medium wp-image-871 aligncenter" title="table_example" src="http://www.macfeteria.com/wp-content/uploads/2009/04/table_example-300x187.png" alt="table_example" width="300" height="187" /></a></p>
<p>และวันนี้เราจะมาลองใช้งาน table view กันครับ</p>
<p>Table View ใน Cocoa touch นั้นต่างกับใน Cocoa ปกติอยู่พอสมควร แต่โครงสร้างหลักๆ และหลักการ รวมถึงวิธีการใช้งานก็เหมือนเดิม ฉนั้นแล้วคนที่เคยใช้ NSTableView มาก่อน ก็หวานหมูเลย จริงๆแล้วข้อแตกต่างที่เห็นได้ชัดๆเลยก็คือ Table View ใน Cocoa touch นั้นสามารถแสดงผลได้ทีละ 1 column เท่านั้น !!! ใน Cocoa touch นั้นจะมี Class เป็น table view ชื่อว่า UITableView และแต่ละ object ที่อยู่ในแถวจะเรียกว่า Cell ( UITableViewCell ) และเนื่องจากว่า cell นั้นเป็น subclass ของ UIView  นั่นหมายความว่า เราสามารถ ใส่ control ต่างๆที่เป็น UIView หรือ subclass ของ UIView เข้าไปใน cell ได้มากมาย อาจจะใส่รูป ใส่ปุ่มกด ตัวหนังสือก็ได้ หรือแม้กระทั่งใส่ table view เข้าไปก็ยังได้ ( เห็นไหมครับว่ามันสามารถปรับแต่งได้สุดๆ )</p>
<p>ก่อนที่เราจะเริ่มผมอยากจะให้กลับไปทำความเข้าใจ เกี่ยวกับการทำงานของ table และ delegate ก่อน</p>
<p>UITableView จะมี helper object ที่ชื่อว่า data source ช่วยในการแสดงผลของข้อมูล ซึ่งการทำงานของ UITableView จะไม่ใช่ลักษณะที่ตัวโปรแกรมเป็นคนสั่งบอกกับ UITableView ว่าให้แสดงคำว่า Saved Photo ในแถวที่หนึ่ง แต่ในทางกลับกัน UITableView มันจะไปถาม helper object เองว่าในแถวที่หนึ่งมีอะไรที่ต้องแสดง ในกรณีนี้ datasource ก็จะส่ง Saved Photo(1) กลับมาให้ยัง NSTable แบบรูปข้างล่าง</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/table.png" rel="lightbox[870]"><img class="size-medium wp-image-874 aligncenter" title="table" src="http://www.macfeteria.com/wp-content/uploads/2009/04/table-300x141.png" alt="table" width="300" height="141" /></a></p>
<p style="text-align: center;">
<p style="text-align: center;">
<p style="text-align: left;">และในการใช้งาน UITableView นั้นเราต้อง implement delegate method ( หากไม่เข้าใจ delegate ควรไปอ่าน <a href="http://www.macfeteria.com/2008/07/cocoa-programming-delegate/">cocoa programming delegate</a> ) ด้วยกัน 2 อย่างคือ</p>
<ul>
<li>-(int) <strong>numberOfRowInTableView</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> NSTableView*) tableView;<br />
ส่งค่าของจำนวน row กลับมา</li>
<li>-(id) <strong>tableView</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> NSTableView *) tableView<br />
<strong>objectValueForTableColumn</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> NSTableColumn*) tableColumn<br />
<strong>row</strong> <img src='http://www.macfeteria.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> int) rowIndex;<br />
ส่ง object ที่ต้องไปแสดงผลที่ row</li>
</ul>
<p>เอาละครับ เมื่อเราได้เข้าพื้นฐานของ UITableView ไปบ้างแล้วลำดับต่อไปเราจะมาเขียนโปรแกรมกัน โดยโปรแกรมที่เราจะเขียนกันในวันนี้ คือโปรแกรมเก็บรายชื่อนักเรียนอย่างง่ายๆกัน โดยหน้าตาโปรแกรมจะเป็นดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/program_table.png" rel="lightbox[870]"><img class="size-medium wp-image-878 aligncenter" title="program_table" src="http://www.macfeteria.com/wp-content/uploads/2009/04/program_table-156x300.png" alt="program_table" width="156" height="300" /></a></p>
<p style="text-align: left;">อธิบายโปรแกรมกันก่อนว่าโปรแกรมเราทำอะไร คือโปรแกรมเราจะมี text field เอาไว้ใส่ชื่อนักเรียน หลังจากนั้นเมื่อเรากด Add รายชื่อนักเรียนก็จะไปปรากฎบนตารางของเรา และสามารถลบได้โดยการกด ไปที่แถวที่ต้องการลบ และกด remove</p>
<h2>Start</h2>
<p>ก่อนอื่นเลยเราก็สร้างโปรเจคแบบ Viewed-Base Application เหมือนๆที่เราเคยทำมาครับ โดยจะตั้งชื่ออะไรก็แล้วแต่ แต่ในโปรเจคนี้ผมตั้งชื่อว่า Student ก็แล้วกัน ก็จะได้โปรเจคหน้าตาแบบในรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/project.png" rel="lightbox[870]"><img class="size-medium wp-image-880 aligncenter" title="project" src="http://www.macfeteria.com/wp-content/uploads/2009/04/project-300x214.png" alt="project" width="300" height="214" /></a></p>
<p>หลังจากนั้นเราจะออกแบบหน้าตาโปรแกรมของเรา โดยเปิด StudentViewController.xib ขึ้นมาครับ แล้วเราก็วาง Control ต่างๆลงไป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/uitable.png" rel="lightbox[870]"><img class="size-medium wp-image-881 aligncenter" title="uitable" src="http://www.macfeteria.com/wp-content/uploads/2009/04/uitable-191x300.png" alt="uitable" width="191" height="300" /></a></p>
<p>จากรูปข้างบน เราได้วาง UITable , UIButton , UITextField เพียงเท่านี้ก็เป็นอันเสร็จการออกแบบ อาจจะงงว่า ทำไมไม่เอา Add , Remove ลงมาอยู่ข้างล่างตาราง เหตุผลก็เพราะว่า เมื่อ Keyboard มันโผล่ขึ้นมา มันจะบัง UITextFields ที่เราได้วางลงไป ผมเลยย้ายมันขึ้นมาไว้ข้างบน</p>
<h2>Code</h2>
<p style="text-align: left;">ก่อนอื่นเลยมาดูในส่วนของ Header กันก่อนว่า เราจะประกาศอะไรบ้าง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> StudentViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
	IBOutlet UITextField <span style="color: #002200;">*</span>m_textField;
	IBOutlet UITableView <span style="color: #002200;">*</span>m_table;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>m_studentList;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>m_studentList;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> delStudent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addStudent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction <span style="color: #002200;">&#41;</span> endEdit<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: left;">ดูจาก code แล้วเราได้ประกาศ outlet มาด้วยกันแค่ 2 ตัวนั่นก็คือ m_textField และ m_table เพื่อที่เราจะได้ติดต่อ UITextFieds และ UITableView ได้ สำหรับในส่วนของ UIButton นั้นเราไม่ได้ต้องการให้มันทำอะไรนอกจาก เรียก action ที่เราต้องการ ฉนั้นก็ไม่ต้องประกาศ outlet ก็ได้ และเราก็ได้ประกาศ action อีก 3 อย่าง ได้แก่ delStudent , addStudent , endEdit ก็แปลตรงๆ ตามชื่อเลยว่าแต่ละ action นั้นเราจะให้มันทำอะไร</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> StudentViewController
<span style="color: #a61390;">@synthesize</span> m_studentList;
&nbsp;
<span style="color: #11740a; font-style: italic;">// ******************* Load &amp;amp; Unload ***************************</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	m_studentList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>m_studentList release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// ******************* Button Action *************************************</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addStudent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> m_textField.text;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>text isNotEqualTo<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
	  <span style="color: #002200;">&#91;</span>m_studentList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> stringWithString<span style="color: #002200;">:</span>text<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span>;
	  <span style="color: #002200;">&#91;</span>m_table reloadData<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> delStudent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span>indexPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_table indexPathForSelectedRow<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> indexPath <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">int</span> row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>m_studentList removeObjectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>m_table reloadData<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// ******************* Text Field Action ********************************</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction <span style="color: #002200;">&#41;</span> endEdit<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>sender resignFirstResponder<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// *************** UITableView Delegate ********************************</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
   numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.m_studentList count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView
   cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>identifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Macfeteria Cell&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell;
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
			reuseIdentifier<span style="color: #002200;">:</span> identifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
	cell.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_studentList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: left;">จาก code ข้างบนผมแบ่งออกเป็น 4 ส่วน นั่นก็ส่วนของ</p>
<ul>
<li>Load &amp; Unload</li>
<li>Button Action</li>
<li>Text Field Action</li>
<li>UITableView Delegate</li>
</ul>
<p>เราจะดูในทีละส่วนก่อนเลยละกัน เริ่มจาก Load &amp; Unload ก่อนละกัน</p>
<h3>Load &amp; Unload</h3>
<p>ในส่วน code ตรงนี้ จะเห็นว่ามี viewDidLoad ซึ่งจริงๆแล้ว viewDidLoad เป็น delegate method ของ UIView ครับ นั่นแปลว่า เมื่อ UIView ของเราถูกโหลดขึ้นมาเสร็จเรียบร้อยแล้ว ก็จะมาถามว่า มีอะไรที่ต้องอีกหรือเปล่า ? ซึ่งก็จะมาอ่าน code ในนี้เหละครับว่า โหลดเสร็จแล้วต้องทำอะไร ถ้าเราดูใน code ภายในก็จะเห็นว่าเราได้บอกว่า หลังจากโหลด UIView นี้ก็ให้จองหน่วยความจำและ init ให้กับ array ของเรานั่นก็คือ m_studentList นั่นเอง และส่วนฟังก์ชั่น dealloc จะถูกเรียกเมื่อโปรแกรมได้จบลง ซึ่งเราก็ได้คืนหน่วยความจำที่เราได้ให้ m_studentList จองไว้นั่นเอง</p>
<h3>Button Action</h3>
<p>action method ในส่วนตรงนี้เราได้เขียน addStudent เพื่อที่ว่า เราจะนำข้อความจาก text field มาใส่ยัง array ของเรานั่นเอง และส่วน delStudent นั้นก็คือเราจะทำการลบข้อมูลที่ได้เลือกไว้ ออกจากตาราง อธิบายเพิ่มเติมในส่วนของ code นิดหนึ่งครับ วิธีการที่เราจะรู้ว่าแถวในตารางไหนจะถูกเลือก จะเห็นว่าเราได้เรียก</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span>indexPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_table indexPathForSelectedRow<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>แต่ indexPath จะยังใช้โดยตรงไม่ได้ เราต้องเรียก [indexPath row] อีกทีเพื่อที่จะได้ตำแหน่งของแถวในตาราง และเมื่อเราได้ลบออกไปแล้ว เราก็ให้ตารางได้ reload เพื่อปรับข้อมูลในการแสดงผล</p>
<h3>Text Field Action</h3>
<p>ในส่วนของ action สำหรับ text field นั้นจริงๆก็ไม่ได้มีอะไรเลยครับ เราแค่ต้องการให้ เมื่อเราได้พิมพ์ข้อความ และหลังจากกด done เรียบร้อยแล้ว ก็จะให้ keyboard นั้นหายไป วิธีการก็คือเราก็ให้ text field ได้ถอนตัวจากการเป็น first responder นั่นเอง</p>
<h3>UITableView Delegate</h3>
<p style="text-align: left;">ก็อย่างที่บอกไปครับว่า เมื่อเราจะใช้ UITableView นั้นเราจำเป็นต้อง implement &#8211; delegate ของ UITableView สอง function โดย delegate funciton แรกคือ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView
   numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> section</pre></div></div>

<p>นั้นเป็นฟังก์ชั่นที่เอาไว้บอกกับ table view ว่ามีจำนวนแถวกี่แถวที่ต้องแสดง และจาก code ที่เราเขียนไป เราก็ได้บอกว่าจำนวนแถวในตารางทั้งหมดของเรา เท่ากับจำนวนของค่าที่เก็บใน m_studentList นั่นเอง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView
   cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;</pre></div></div>

<p>สำหรับ delegate method นี้เป็นฟังก์ชั่นที่เอาไว้บอกว่า ข้อมูลที่จะแสดงในแถวของ ตาราง คืออะไร  ** และอย่างที่บอกไปแล้วว่า แถวของตารางนั้นเป็น control ที่เรียกว่า UITableViewCell ** ฉนั้นแล้วเราต้องสร้าง UItableViewCell ขึ้นมาก่อน</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero
			reuseIdentifier<span style="color: #002200;">:</span> identifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>และจากบรรทัดนี้ จะเห็นว่าเราได้ประกาศ cell ขึ้นมาและได้จองหน่วยความจำให้กับ cell พร้อมกับ init เรียบร้อย อาจจะงงตรง ที่เขียนว่า reuseIndentifier ว่ามันคืออะไร คือ UITalbleViewCell นั้นเป็น Subclass ของ UIView นั่นแปลว่า cell ของเรานั้นอาจจะเป็น ข้อความธรรมดา หรืออาจจะเป็น Custom View หรือแม้กระทั่งเป็น table ทั้งอันก็ยังได้ ฉนั้นการที่ มี indentifier ก็เพื่อระบุว่า cell นี้เป็นประเภทไหน ความสำคัญอีกอย่างก็เพื่อจะได้นำกลับมาใช้ใหม่ซึ่งจะอธิบายในลำดับต่อไป</p>
<p>เมื่อเรามี cell แล้วลำดับต่อไปเราก็จะนำข้อมูลที่ต้องการใส่ไปใน cell และข้อมูลที่เราต้องการในที่นี้ก็คือข้อมูลที่เราได้เก็บไว้ในตัวแปร m_studentList นั่นเอง</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	NSUInteger row <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span>;
	cell.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>m_studentList objectAtIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> cell;</pre></div></div>

<p>ยังมี code ส่วนที่เหลือนั่นก็คือ</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>identifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Macfeteria Cell&quot;</span>;
	UITableViewCell <span style="color: #002200;">*</span>cell;
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>อธิบายเพิ่มเติมนะครับว่า เมื่อเราได้ใช้นิ้วเลื่อนดูค่าในตำแหน่งอื่นๆของตาราง Cell ที่ได้ถูกเลื่อนไปจนไม่สามารถจะแสดงผลที่หน้าจอได้ ตามหลักการทำงานของโปรแกรมแล้ว มันควรจะคืนหน่วยความจำของ Cell นั้นให้กับระบบ แล้วก็จองหน่วยความจำใหม่ ให้กับ Cell ที่จะมาแทนที่ Cell เดิมที่ได้หายไปจากหน้าจอ แต่เนื่องจากว่า iPhone นั้นค่อนข้างจะจำกัดทรัพยากรของระบบ วิศวกรของ Apple เลยออกแบบให้ Cell ที่ได้หายไปจากหน้าจอจะยังไม่ถูกทำลาย แต่จะถูกเก็บไว้ยัง dequeue เพื่อที่ว่าจะได้นำกลับมาใช้ใหม่ ซึ่งจะช่วยลดการจองและคืนหน่วยความจำให้กับระบบบ่อยๆ  ดังนั้นการใช้</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>identifier<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>ก็เพื่อเพิ่มประสิทธิภาพการใช้งานหน่วยความจำให้กับระบบนั่นเอง สรุปก็คือ ก่อนที่มันจะจองหน่วยความจำใหม่ มันจะไปถาม table ก่อนว่ามี cell ไหนที่สามารถนำกลับมาใช้ได้บ้างหรือเปล่า ถ้ามี ก็ไม่ต้องจองใหม่ แต่ถ้าไม่มี ก็จองหน่วยความจำใหม่นั่นเอง</p>
<h2>Link Control</h2>
<p>ลำดับต่อไปเราก็จะ link control ต่างๆให้เข้ากับ action ของเราครับ โดยกลับไปที่ interface builder แล้วก็ เชื่อม</p>
<ul>
<li>m_table กับ UITableView</li>
<li>m_textField กับ UITextField</li>
</ul>
<p>ผมคิดว่าใน tutorial นี้คงไม่ต้อง อธิบายเยอะนะครับว่า เราจะเชื่อม Control และ Action ยังไง ขออธิบายคร่าวๆเลยนะครับ แล้วก็ link action ให้กับ Button ของเรา รวมถึง action ของ m_textField ด้วย</p>
<p>มีส่วนเพิ่มเติมก็คือใน connection inspector ของ UITableView นั้นจะเห็น Delegate และ Data Source ดังรูปครับ ให้ลากเชื่อมเข้ากับ File&#8217;s Owner ดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/datasource.png" rel="lightbox[870]"><img class="size-medium wp-image-900 aligncenter" title="datasource" src="http://www.macfeteria.com/wp-content/uploads/2009/04/datasource-300x237.png" alt="datasource" width="300" height="237" /></a></p>
<p>ที่เราทำตรงนี้ก็เพื่อว่าจะได้บอกให้ table ของเราได้รู้ว่า object ไหนเป็น helper object และ datasource นั่นเอง<br />
ส่วน Action  อื่นๆ เช่น delStudent ก็ลากเข้ากับปุ่ม ให้เรียบร้อย และรวมถึง endEdit ที่ต้องเชื่อมกับ text filed ด้วยนะครับ ถ้าหากเสร็จหมดแล้ว ถ้า click ขวาที่ File&#8217;s Owner ก็จะเห็น panel พร้อมกับค่าต่างๆดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/completed.png" rel="lightbox[870]"><img class="size-medium wp-image-901 aligncenter" title="completed" src="http://www.macfeteria.com/wp-content/uploads/2009/04/completed-300x200.png" alt="completed" width="300" height="200" /></a></p>
<p>หลังจากก็ลอง Compile &amp; Run ดูครับ เราก็จะสามารถ พิมพ์ข้อความและกด Add เพื่อให้ข้อความนั้นเข้ามายัง table ของเราได้แล้ว</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/add_row.png" rel="lightbox[870]"><img class="size-medium wp-image-902 aligncenter" title="add_row" src="http://www.macfeteria.com/wp-content/uploads/2009/04/add_row-156x300.png" alt="add_row" width="156" height="300" /></a></p>
<p>และแน่นอนว่า เราสามารถลบข้อมูลได้ด้วยการเลือกไปยังแถวที่ต้องการและกด remove ดังรูป</p>
<p style="text-align: center;"><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/remove_row.png" rel="lightbox[870]"><img class="size-medium wp-image-903 aligncenter" title="remove_row" src="http://www.macfeteria.com/wp-content/uploads/2009/04/remove_row-156x300.png" alt="remove_row" width="156" height="300" /></a></p>
<p>เป็นอย่างไรบ้างครับสำหรับ tutorial นี้คงได้ไปลองๆหัดทำกัน และได้เข้าใจเกี่ยวกับ delegate พร้อมกับ UITableView กันนะครับ ยินดีรับคำแนะนำ และติชมนะครับ</p>
<p><a href="http://www.macfeteria.com/wp-content/uploads/2009/04/iphone_student.zip">Download Source :: iPhone &#8211; Student List With UITable<br />
</a></p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/cocoa+touch' rel='tag' target='_self'>cocoa touch</a>, <a class='technorati-link' href='http://technorati.com/tag/iphone' rel='tag' target='_self'>iphone</a>, <a class='technorati-link' href='http://technorati.com/tag/programming' rel='tag' target='_self'>programming</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableView' rel='tag' target='_self'>UITableView</a>, <a class='technorati-link' href='http://technorati.com/tag/UITableViewCell' rel='tag' target='_self'>UITableViewCell</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.macfeteria.com/blog/2009/04/09/iphone-programming-iv-table-view-path-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

