Custom TableViewCell

การออกแบบ TableCell ใน Table

ขั้นแรกสร้าง UITableView ขึ้นมาก่อน

หลังจากนั้นสร้าง File ใหม่โดย

Add -> New File -> Objective-C class ตรง Subclass of เลือก UITableViewCell

สร้าง Interface FIle (.xib) ออกแบบ UI

สร้าง Outlet กำหนด Property และทำ Synthesize

เข้าไปที่ FIle ViewController.m ที่มี UITableView

#import “MyTableViewCell.h”

ใน Method ของ DataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

เราจะทำการสร้าง UITableViewCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell == nil) {

NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@”TableCell” owner:self options:nil];

for (int i = 0; i < [cellArray count]; ++i) {

id obj = [cellArray objectAtIndex:i];

if ([obj isKindOfClass:[MyTableViewCell class]]) {

cell = obj;

cell.cellLabel.text = [NSString stringWithFormat:@"%d", i];

cell.backgroungImage = [UIImage imageNamed:@"cellBackgroundImage.png"];

}

}

}

return cell;

}

การกำหนดความสูงของ TableViewCell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100.0f;

}

  1. Yeaw
    ตุลาคม 12, 2010 ที่ 4:10 pm | #1

    อ่านแล้วทำตามไม่ได้เลยครับ ไม่เคลียร์ตรงสร้าง uitableview และออกแบบ .xib ครับ แล้วต้องสร้าง outlet ของอะไรก็ไม่รู้ครับ จริงๆอ่านถึงบรรทัดที่สองก็ทำต่อไม่ได้แล้ว T_T

    • พฤศจิกายน 12, 2010 ที่ 7:01 am | #2

      เดี๋ยวผมย้าย Blog แล้วจะอธิบายรายละเอียดให้มากขึ้นนะครับ
      ออกแบบ .xib คือออกแบบหน้าตาของ Table cell ครับ
      ส่วน Outlet ก็เป็นตัวเชื่อมระหว่างค่าใน Code กับหน้าตาที่เราออกแบบไว้ครับ

  1. No trackbacks yet.

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Connecting to %s

Follow

Get every new post delivered to your Inbox.