UITableView 兩個獲得重用cell方法的區別

1,

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

2,

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered


注釋中可以看出的端倪,第一個是老的接口,第二個新一點,在使用結構上:

第一個方法在

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

要這么寫

cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kTableviewIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:kTableviewIdentifier];

}


第二個方法要這么處理

在定義UITableView位置:

[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableviewIdentifier];


然后在

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

就省掉了判斷為空,可以直接使用

cell = [tableView dequeueReusableCellWithIdentifier:kTableviewIdentifier forIndexPath:indexPath];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容