Xib使用之TableViewCell.xib中建立多個Cell
轉:https://www.jianshu.com/p/332e1db6ebb5
loongod 12016.04.03 16:34:35字數 912閱讀 19,146初次使用xib
建立UITableviewCell
的時候,我都是一個xib
檔案裡,只建立一個Cell
,在實際業務中,往往都是一個列表中需要用到多個不同的Cell
樣式,這就需要建立N個.h .m .xib
檔案。而且這些.m
中的實現還差不多。
後來發現,一個.xib
檔案中可以建立多個Cell
,如圖:
這樣感覺方便多了。
具體實現:
第一步建立
先和普通建立xibCell
一樣,在xib
中選中左邊那個Cell
UITableViewCell
上去,然後paste(command + v),.xib
中就多個Cell
了,O(∩_∩)O~~
多個Cell
第二步設定Identifier和程式碼使用
在程式碼中建立Cell
時
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] firstObject];
}
TempTableViewCell
是你的xib
檔名,firstObject
Cell
,按順序排的。第二個怎麼辦??
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:2];
再多依次類推哈。(提示:如果在Cell中新增手勢的話,loadNibNamed:
這個返回的陣列中會比Cell多哦,大家注意)
設定每個Cell的identifier,(identifier 隨意起的,我的規律就是類名+第幾,不要重複就行)如圖:
設定每個Cell的identifier這樣在重用佇列中重複使用Cell的時候,能找到正確的Cell,
TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TempTableViewCellFirst"];
可以根據indexPath設定不同的identifier。
可以把建立Cell的過程放在Cell.m中,做成類方法,這樣不至於VC中的程式碼過多。
cell.h中:
cell.m中:
+ (instancetype)tempTableViewCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"";//對應xib中設定的identifier
NSInteger index = 0; //xib中第幾個Cell
switch (indexPath.row) {
case 0:
identifier = @"TempTableViewCellFirst";
index = 0;
break;
case 1:
identifier = @"TempTableViewCellSecond";
index = 1;
break;
case 2:
identifier = @"TempTableViewCellThird";
index = 2;
break;
default:
break;
}
TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:index];
}
return cell;
}
這樣VC中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TempTableViewCell *cell = [TempTableViewCell tempTableViewCellWith:tableView indexPath:indexPath];
return cell;
}
是不是很方便呢。
設定屬性
快捷設定xib屬性:
1. 在.h或者.m中先寫好屬性,如圖:
設定屬性2. 在xib中就可以拖拽連線屬性了,如圖:
property.gif重點:關聯屬性的時候,你想關聯那個Cell上的屬性,需要先點選左邊Cell列表,選中該Cell,然後再拖線關聯上面的控制元件。
設定好屬性,下面就是使用了,
配置Cell
/**
* @author god~long, 16-04-03 16:04:04
*
* 配置TempCell的方法
*
* @param indexPath 對應TableView的indexPath
*/
- (void)configTempCellWith:(NSIndexPath *)indexPath;
.m中:
- (void)configTempCellWith:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0: {
_label1.text = @"我是Label1";
_customImageView.image = [UIImage imageNamed:@"8"];
break;
}
case 1: {
_label2.text = @"我是Label2";
[_customButton setTitle:@"我是button" forState:UIControlStateNormal];
break;
}
case 2: {
_label1.text = @"我是第三個Cell的Label1";
_label2.text = @"我是第三個Cell的Label2";
break;
}
default:
break;
}
}
重點:每個Cell設定連結的屬性都是單獨處理的,沒連,在用的時候即使你用了,也設定不了。
執行效果:
深入
新建一個UITableViewCell
建立的時候選擇同時建立xib
,然後
右擊cell.xib檔案➡Open As➡Souce Code 如下圖
然後可以看到xib
檔案其實是以xml
資料格式儲存的,如圖:
當我改變xib
中1和2兩個cell
的順序之後(可以雙指拖動),然後檢視他們的source code
會發現,他們在xml
檔案中的順序也會改變。用Bundle.main.loadNibNamed("*xibName*", owner: self, options: nil)
方法返回陣列的Item
順序也會改變。
我們在呼叫let cells = Bundle.main.loadNibNamed("*xibName*", owner: self, options: nil)
的時候,載入的是xml
中<objects/>
標籤下的資料,其中<placeholder>
標籤下中如果placeholderIdentifier="IBFilesOwner"或者"IBFirstResponder"
時會被忽略。也就是不會返回到上面的cells陣列中。但是如果我新增一個手勢的話,就會返回到陣列中了。
具體如下圖:
其中<objects>
中的預設資料順序是根據我們在xib
中新增控制元件的順序而定的,下面我又先添加了一個tap
手勢,然後又添加了一個cell
,檢視source code
和列印cells
資料如下圖:
回答關於重用
我在建立cell的判斷裡面
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:index];
NSLog(@"create cell indexPath: %ld",index);
}
然後
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
執行並滑動之後截圖如下:
介面 控制檯So,是會重用滴。
dome地址:點我點我