dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier : forIndexPath
StackOverFlow連結
方法名介紹
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__kindof UITableViewCell *)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
區別
這兩個方法都是在tableView:cellForRowAtIndexPath
這個方法裡面獲取cell用的
1.對於dequeueReusableCellWithIdentifier:forIndexPath
如果沒有給複用的id註冊一個class或者nib的話,那麼程式就會crash
2.對於dequeueReusableCellWithIdentifier
如果沒有給複用id註冊一個class或者nib的話,那麼就會返回nil
因此對於老的方法
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 需要手動判斷是否為nil
if (cell == nil) {
//建立cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
對於新的方法
dequeueReusableCellWithIdentifier:forIndexPath
//第一步 註冊 一個通過xib 一個通過class。隨便選一個
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
//第二步
// 不需要判斷cell是否為nil,該新方法如果找不到cell,會自動呼叫
// initWithStyle:withReuseableCellIdentifier 建立一個新的
// 因此,必定不為空
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify forIndexPath:indexPath];
// 那麼引數indexpath有什麼用?
// 因為在返回cell之前,會呼叫委託ableView:heightForRowAtIndexPath來確定cell尺寸(如果已經定義該函式)。
1.The most important difference is that the forIndexPath: version asserts (crashes) if you didn’t register a class or nib for the identifier. The older (non-forIndexPath:) version returns nil in that case.
2.the (then-new) forIndexPath: version starting around 8m30s. It says that “you will always get an initialized cell” (without mentioning that it will crash if you didn’t register a class or nib).
3.The video also says that “it will be the right size for that index path”. Presumably this means that it will set the cell’s size before returning it, by looking at the table view’s own width and calling your delegate’s tableView:heightForRowAtIndexPath: method (if defined). This is why it needs the index path.
相關文章
- 轉:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]AIUIViewIndex
- Playwright VS Selenium VS Puppeteer VS Cypress
- vs 2017 vs code
- Airflow vs. Luigi vs. Argo vs. MLFlow vs. KubeFlowAIUIGo
- Axum vs Actix vs Rocket
- RDBMS VS XML VS NoSQLXMLSQL
- 如何解除安裝VS 2017之前版本比如VS 2013、VS2015、 VS vNext?
- [譯]await VS return VS return awaitAI
- The SQL vs NoSQL Difference: MySQL vs MongoDBMySqlMongoDB
- HashSet vs. TreeSet vs. LinkedHashSet
- Redux vs Mobx系列(-):immutable vs mutableRedux
- spring vs yii2 vs LaravelSpringLaravel
- coca 搭配 in vs on vs at | page1
- coca 搭配 in vs on vs at | page3
- JavaScript 的 4 種陣列遍歷方法: for VS forEach() VS for/in VS for/ofJavaScript陣列
- ABAP vs Java, 蛙泳 vs 自由泳Java
- When to use var vs let vs const in JavaScriptJavaScript
- Tomcat vs Jetty vs Undertow效能對比TomcatJetty
- 微軟常用執行庫合集下載(vs2008(sp)/vs2010(sp)/vs2012/vs2013/vs2015/vs2017)包含32位/64位微軟
- 測試速度比較:Selenium vs Playwright vs Cypress vs Puppeteer vs TestCafe
- javascript — == vs ===JavaScript
- vs 2017
- PostgreSQL DBA(6) - SeqScan vs IndexScan vs Bit...SQLIndex
- PostgreSQL DBA(131) - Develop(numeric vs float vs int)SQLdev
- 計數排序vs基數排序vs桶排序排序
- iOS:原生應用 VS Flutter VS GICXMLLayout 比較iOSFlutterXML
- PostgreSQL DBA(121) - pgAdmin(HA:PAF vs repmgr vs Patroni)SQL
- 如何實現 “defer”:Go vs Java vs C/CPPGoJava
- Flutter VS React Native VS Native,誰才是效能之王FlutterReact Native
- Go vs Java vs C# 語法對比GoJavaC#
- 資料湖 vs 倉庫 vs 資料庫資料庫
- 我將從VS Code切換到VS Codium
- 資料質量管理工具預研——Griffin VS Deequ VS Great expectations VS Qualitis
- computed vs watch
- transform Vs UdfORM
- HBase vs HiveHive
- vs整合nasmASM
- encodeURI VS encodeURIComponent
- defer vs return