2017-06-22 69 views
0

當我忽略爲我的UITableView註冊給定單元格,但稍後在func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell中調用dequeueReusableCell時發生了什麼?UITableView忽略註冊單元格

調用看起來是這樣的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell` 
    var cell = tableView.dequeueReusableCell(withIdentifier: kIdentifier) as? MyCellType 

    if cell.isNil { 
     cell = MyCellType(withIdentifier: kIdentifier) 
    } 
} 

顯然,它似乎自動註冊新的細胞。這個假設是否正確?

我之所以可以撥打tableView.register(MyCellType.self, forCellReuseIdentifier: kIdentifier) has legacy reasons, where registering the cell like this will mess up the layout of the cell, whereas initializing correctly with MyCellType(...)``不會。

回答

0

你的假設是正確的。 我經常在Object-C中編寫這個樣式代碼,很好。

0

你需要檢查空單元格如下:

if (cell == nil) { 
    cell = MyCellType(withIdentifier: kIdentifier) 
    } 
+0

我已經有了這個檢查。或者爲什麼我不應該使用'cell.isNil'? –

+0

那麼會發生什麼問題? – KKRocks

+0

你使用prototypecell還是xib? – KKRocks