ios中自定義table view cell

2021-06-09 05:55:05 字數 3081 閱讀 9126

跟著書上的範例做完了一般的table view,然後做做自定義**,也遇到一些問題,最後終於解決了,記錄下怎麼弄出來的吧。

知識點:

1.自定義**cell的格式。

2.自定義**cell的view類。

3.使用自定義的view來呈現資料。

一、自定義**cell的格式

新建xib檔案,拖table view cell,label,image view到畫板上,如圖:

二、自定義**cell的view類

新建objective-c class,繼承uitableviewcell類,見**:

//

// ******tableviewcellcontroller.h

// ******tablestoryboradtest

//// created by wong linwei on 12-9-8.

//#import @inte***ce ******tableviewcell : uitableviewcell

@property (strong, nonatomic) iboutlet uiimageview *cellimage;

@property (strong, nonatomic) iboutlet uilabel *cellname;

@property (strong, nonatomic) iboutlet uilabel *celltime;

@end

//

// ******tableviewcellcontroller.m

// ******tablestoryboradtest

//// created by wong linwei on 12-9-8.

//#import "******tableviewcell.h"

@implementation ******tableviewcell

@synthesize cellimage;

@synthesize cellname;

@synthesize celltime;

- (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier

return self;

}- (void)setselected:(bool)selected animated:(bool)animated

@end

三、使用自定義view類

在有table view的view controller類中實現uitableviewdelegate,uitableviewdatasource兩個協議。

//

// pntviewcontroller.h

// ******tablestoryboradtest

//// created by wong linwei on 12-9-5.

//#import @inte***ce pntviewcontroller : uiviewcontroller

@end

//

// pntviewcontroller.m

// ******tablestoryboradtest

//// created by wong linwei on 12-9-5.

//#import "pntviewcontroller.h"

#import "******tableviewcell.h"

@implementation pntviewcontroller

- (void)didreceivememorywarning

#pragma mark - view lifecycle

- (void)viewdidload

- (void)viewdidunload

- (bool)shouldautorotatetointe***ceorientation:(uiinte***ceorientation)inte***ceorientation

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath

cell.cellname.text = [tabledata objectatindex:indexpath.row];

cell.celltime.text = [nsstring stringwithformat:@"%@",[nsdate date]];

cell.cellimage.image = [uiimage imagenamed:@"titlepic.png"];

return cell;

}-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

@end

看下最後的結果吧:

出現的問題是:

1.執行是出現空白的**,這種情況的發生,一是datadelegate和datasource兩個iboutlet沒有連線上,二是因為給cell取的identifier不要跟復用的那個identifier一樣。

2.在載入自定義cell布局的時候,發生crash現象,因為cell取得identifier跟復用的identifier一樣了。

記得如果出不來,就去模擬器裡面停止應用並且刪除以後再run即可。

ios自定義字型

提供字型檔案路徑,返回所需要字型 複製 uifont customfontwithpath nsstring path size cgfloat size 這樣就不需要在plist設定任何東西,只需要得到字型庫檔案的路徑,就可以取出對應的字型。上面的方法對於ttf otf的字型都有效,但是對於ttc...

iOS 自定義鬧鐘

ios 自定義鬧鐘 需求 從伺服器獲取到一組時間,都設定成鬧鈴,預設是每天迴圈不可修改,同時可以自定義鬧鈴,可選擇週幾迴圈。可以設定開啟關閉或者刪除。一.固定鬧鐘,每天迴圈。很簡單,將需要設定成鬧鐘的時間放入乙個陣列中,通過for迴圈設定成對應的本地推送,詳情見下面的 和注釋。void addloc...

IOS 自定義協議

在自定義協議的標頭檔案 h中 protocol nsdelegate optional 可選 void onoption nsstring pstr required 必須 void onrequired nsstring pstr end inte ce nsclass nsobject id d...