iOS 自定義cell及兩種復用方式

2021-10-08 07:24:58 字數 2487 閱讀 7574

首先,在這裡反省一下自己,在遇到問題的時候沒有及時去解決,這一點很不好,得改

//嘗試獲得可以復用的單元格。但不一定獲得到。得不到返回nil

uitableviewcell *cell =

[_tableview dequeuereusablecellwithidentifier:

@"ft"];

//如果沒有獲得到

if(cell == nil)

[

self

.tableview registerclass:

[mytableviewcell class] forcellreuseidentifier:

@"ft"];

mytableviewcell *cell =

[self

.tableview dequeuereusablecellwithidentifier:

@"ft" forindexpath:indexpath]

;

建立mytableviewcell檔案繼承於uitableviewcell

// mytableviewcell.**件

@inte***ce mytableviewcell : uitableviewcell

@property uilabel *label;

@property uibutton *btn;

@end

檔案

#import "mytableviewcell.h"

@implementation mytableviewcell

-(instancetype)initwithstyle:

(uitableviewcellstyle)style reuseidentifier:

(nsstring *

)reuseidentifier

return

self;}

//布局-(

void

)layoutsubviews

@end

在viewcontroller.h 中新增協議 定義資料檢視物件

#import

@inte***ce viewcontroller : uiviewcontroller

<

//實現資料檢視的普通協議

//資料檢視的普通事件處理

uitableviewdelegate,

//實現資料檢視的資料**協議

//處理資料檢視的資料**

uitableviewdatasource

>

//定義資料檢視物件。資料檢視用來顯示大量相同格式的大量資訊的檢視

@property uitableview *tableview;

@end

viewcontroller.m檔案:

#import "viewcontroller.h"

#import "mytableviewcell.h"

@inte***ce viewcontroller (

)@end

@implementation viewcontroller-(

void

)viewdidload

//組數

-(nsinteger)numberofsectionsintableview:

(uitableview *

)tableview

//組內行數

-(nsinteger)tableview:

(uitableview *

)tableview numberofrowsinsection:

(nsinteger)section

//單元格高度

-(cgfloat)tableview:

(uitableview *

)tableview heightforrowatindexpath:

(nsindexpath *

)indexpath

//設定單元格

-(uitableviewcell *

)tableview:

(uitableview *

)tableview cellforrowatindexpath:

(nsindexpath *

)indexpath

//以上為uitableview 重用cell兩種方法

if(indexpath.section ==0)

else

return cell;

}@end

執行:

載入自定義 cell 的 XIB 檔案 的兩種方式

自定義單元格如果是使用 ib 方式建立的,則需要手動載入,因為 xib 上的 cell 不會自動載入 第一種 使用應用程式束 應用程式束 nsbundle 獲得工程中所有的資源的路徑,相當於當前工程的目錄。獲得指定的xib中所有的檢視 nsarray array nsbundle mainbundl...

Android 兩種自定義的Menu

雖然兩種方法有很多相似的地方,但是使用的方法不同,廢話不多說,帖方法了 方法一功能簡述 要實現能代替系統選單的自定義選單必須保證以下基本功能的實現 能通過menu鍵隱藏顯示選單 截獲系統menu鍵動態隱藏顯示 當獲得焦點時高亮顯示獲得焦點的選單項 實現按下選單狀態效果 能設定選單背景 能設定選單項背...

自定義dialog的兩種方式

介紹自定義對話方塊 dialog 的兩種方式 方式一 首先自定義乙個自己想要設計的對話方塊的介面布局,我的是這樣的 然後在activity類中進行展示,這裡我寫了乙個showinputpwddialog方法 輸入密碼對話方塊 private void showinputpwddialog 方式二 可...