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
- 轉:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]AIUIViewIndex
- NOSQL資料庫大比拼:Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBaseSQL資料庫MongoDBRedis
- Airflow vs. Luigi vs. Argo vs. MLFlow vs. KubeFlowAIUIGo
- RDBMS VS XML VS NoSQLXMLSQL
- Axum vs Actix vs Rocket
- TensorFlowVSTensorFlowMobileVSTensorFlowLite
- vs 2017 vs code
- EncodedvsLiteral,RPCvsDocumentRPC
- The SQL vs NoSQL Difference: MySQL vs MongoDBMySqlMongoDB
- Redux vs Mobx系列(-):immutable vs mutableRedux
- MVC vs. MVP vs. MVVMMVCMVPMVVM
- 轉:malloc VS new, free VS deletedelete
- 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陣列
- Python Flask vs Ruby Sinatra vs Go Martini vs Node ExpressPythonFlaskGoExpress
- spring vs yii2 vs LaravelSpringLaravel
- ABAP vs Java, 蛙泳 vs 自由泳Java
- Java 集合 ArrayList VS LinkedList VS VectorJava
- 普通Webview vs X5 vs sonicWebView
- IVs提取合併工具ivstools
- 如何選擇前端框架:ANGULARVSEMBERVSREACT前端框架AngularReact
- javascript — == vs ===JavaScript
- vsftpdFTP
- VSFTPFTP
- VSCodium:100% 開源的 VS Code
- [譯]await VS return VS return awaitAI
- When to use var vs let vs const in JavaScriptJavaScript
- LVS - ipvsadm命令參考
- c語言初學者用vs還是vscode vs和vscode哪個適合初學者C語言VSCode
- 如何實現 “defer”:Go vs Java vs C/CPPGoJava
- LVS 負載均衡之 VS/NAT 模式負載模式
- LVS 負載均衡之 VS/TUN 模式負載模式
- LVS 負載均衡之 VS/DR 模式負載模式
- 計數排序vs基數排序vs桶排序排序
- iOS:原生應用 VS Flutter VS GICXMLLayout 比較iOSFlutterXML
- 資料湖 vs 倉庫 vs 資料庫資料庫