跳到主要內容

[iOS] Objective-C 自訂UITableViewCell樣式

Step1 在Storyboard中拉出TableView和按下TableView Cell後要切換的View,並設定對應的class檔

TableView連結至ListController類別,並設定Storyboard ID為ListController

要切換的View連結至ViewController類別,並設定Storyboard ID為viewController


Step2 新增CustomCell.xib

新增File(File → New → File...),選擇View,並將檔案命名為CustomCell

開啟CustomCell.xib,把預設的View元件刪掉,然後從元件庫拖曳Table View Cell,並拉好想要的Cell內容排版

Step3 新增自訂Objective-C class給這個NIB檔

新增File(File → New → File...),選擇Cocoa Touch class

需要繼承UITableViewCell

Step4 回到CustomCell.xib,將Class連結到CustomTableViewCell


Step5 在CustomTableViewCell.h檔中,設定元件變數

@interface CustomTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *devName;
@property (weak, nonatomic) IBOutlet UILabel *info1;
@property (weak, nonatomic) IBOutlet UILabel *info2;
@property (weak, nonatomic) IBOutlet UILabel *info3;
@property (weak, nonatomic) IBOutlet UILabel *info4;
@property (weak, nonatomic) IBOutlet UILabel *info5;
@property (weak, nonatomic) IBOutlet UILabel *info6;
@end

Step6 在ListController.h中設定TableView的元件變數,且宣告一個陣列,負責儲存表格上要呈現的資料

#import <UIKit/UIKit.h>
@interface ListController : UIViewController{
NSMutableArray *list;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

Step7 在ListController.m  的viewDidLoad方法中設定TableView的兩個輸出口:dataSource、delegate,並初始化陣列資料

dataSource所連結到的View Controller用來提供表格上Cell的內容
delegate為指定哪一個View Controller需要處理使用者在表格上的操作
- (void)viewDidLoad {
[super viewDidLoad];
_tableView.delegate = self;
_tableView.dataSource = self;
list = [[NSMutableArray alloc] init];
[list addObject:[NSArray arrayWithObjects:@"Name1",@"info1",@"info2",@"info3",@"info4",@"info5",@"info6", nil]];
[list addObject:[NSArray arrayWithObjects:@"Name2",@"info1",@"info2",@"info3",@"info4",@"info5",@"info6", nil]];
[list addObject:[NSArray arrayWithObjects:@"Name3",@"info1",@"info2",@"info3",@"info4",@"info5",@"info6", nil]];
[list addObject:[NSArray arrayWithObjects:@"Name4",@"info1",@"info2",@"info3",@"info4",@"info5",@"info6", nil]];
}

Step8 在ListController.m中,實作tableView:numberOfRowsInSection:方法-通知Table View需要產生多少個Cell(儲存格)來顯示資料

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}

Step9 在ListController.m中,實作tableView:cellForRowAtIndexPath:方法-實際建立Cell,並將要呈現在Table View中的資料放進Cell中

1,利用dequeueReusableCellWithIdentifier:方法取得可再利用的Cell,不需要每次都花時間產生一個新的
2,載入CustomCell.xib檔,從中取得自訂儲存格,最後將資料放入Label內
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
//自訂Cell的類別
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
//載入CustomCell.xib檔
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (UIView *view in views) {
if ([view isKindOfClass:[CustomTableViewCell class]]){
cell = (CustomTableViewCell *)view;
}
}
}
//填寫資料到Label元件中
cell.devName.text = [[list objectAtIndex:indexPath.row] objectAtIndex:0];
cell.info1.text = [[list objectAtIndex:indexPath.row] objectAtIndex:1];
cell.info2.text = [[list objectAtIndex:indexPath.row] objectAtIndex:2];
cell.info3.text = [[list objectAtIndex:indexPath.row] objectAtIndex:3];
cell.info4.text = [[list objectAtIndex:indexPath.row] objectAtIndex:4];
cell.info5.text = [[list objectAtIndex:indexPath.row] objectAtIndex:5];
cell.info6.text = [[list objectAtIndex:indexPath.row] objectAtIndex:6];
return cell;
}

Step10 在ListController.m中,實作tableView:heightForRowAtIndexPath:方法-因為自訂的Cell有更改高度,所以要將高度改為與自訂Cell相同的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 220;
}

Step11 在ListController.m中,實作tableView:didSelectRowAtIndexPath:方法-設定Cell按下後的事件

按下後,會切換頁面至Storyboard上ID為"ViewController"的頁面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController *vc ;
switch (indexPath.row)
{
case 0:
vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"ViewController"];
[self presentModalViewController:vc animated:YES];
break;
case 1:
vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"ViewController"];
[self presentModalViewController:vc animated:YES];
break;
}
}

執行結果:

留言

這個網誌中的熱門文章

[Android] 使用shape自訂形狀及陰影

shape可以定義下面四種類型的形狀:     rectangle- 矩形(直角矩形、圓角矩形),默認的形狀     oval- 橢圓形、圓形     line- 線形(實線、虛線)     ring- 環形、環形進度條 rectangle 執行結果: 如果填色要填入漸層色的話,需要將solid標籤改成gradient標籤,用法如下: 執行結果: oval 執行結果: oval和ring的漸層有三種類型(android:type):linear(線性)、radial(放射性)、sweep(掃描) 如果漸層效果為radial(放射性)的話,必須加上android:gradientRadius屬性(指定漸層的半徑)。 執行結果: line 線的高度是透過stroke的android:width屬性設置,而size的android:height是定義整個形狀區域的高度,所以 size的height必須大於stroke的width 。 執行結果: ring 屬於ring的屬性:     android:innerRadius 內環的半徑     android:thickness 環的厚度     android:useLevel 一般設為false,要不然環可能會無法顯示 執行結果: 將環設計成環狀進度條的樣式: 1,在shape標籤外再加上rotate標籤 rotate屬性:     android:fromDegrees:開始旋轉的角度位置     android:toDegrees:結束時轉到的角度位置     android:pivotX:旋轉起點的X軸座標位置,可以是數值、百分比、百分比p     android:pivotY:旋轉起點的Y軸座標位置,可以是數值、百分比、百分比p 2,在style.xml裡加上style樣式 3,在activity_main.xml裡加上progressBar標籤...

[隋堂筆記] Android 利用BaseAdapter來自訂ListView

BaseAdapter 可以讓使用者自己定義許多種 View ,像是 Spinner, ListView, GridView等,而且 可以重用View來節省資源 :使用setTag將View存起來,之後就可以利用getTag將數據取出來 Step1  在activity_main.xml中新增ListView 例如: Step2 在layout中新增list_content.xml,並排好想要的ListView item佈局 Step3 在MainActivity.java 新增一個類別BA並繼承BaseAdapter 透過繼承 BaseAdapter,我們可以重新覆寫4個方法:     public int getCount();     取得 ListView 列表 Item 的數量。通常數量就是從建構子傳入的陣列或是集合大小。     public Object getItem(int position);     取得 ListView 列表於 position 位置上的 Item。position 通常是資料在陣列或是集合上的位置。     public long getItemId(int position);     取得 ListView 列表於 position 位置上的 Item 的 ID,一般用 position 的值即可。     public View getView(int position, View view, ViewGroup viewGroup);     通常會設定與回傳View 作為顯示在這個 position 位置的 Item 的 View。 基本上只要處理兩個方法 getCount跟getView即可。 getCount回傳我所宣告的String陣列st1的長度。 在getView之前,要先取得LayoutInflater 再來要先宣告一個類別viewHolder,用來存放list_content.xml內所有元件的內容 接著判斷view是否為空,如果為空就要...