问题
According to the official documentation, there are two ways to get a reusable cell from a queue of a tableView. One is dequeueReusableCell(withIdentifier:for:)
and another is dequeueReusableCell(withIdentifier:)
. Assuming from the explanation of the document, I think the former is the method that returns the reusable cell and adds it to tableView
. On the other hand, the latter is the method that just returns the reusable cell.
Is this right?
If it is right, I have another question.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCell(with: SomeTableViewCell.self, for: indexPath)
let anotherCell: UITableViewCell = self.tableView.dequeueReusableCell(with: AnotherTableViewCell.self)
return anotherCell
}
At first line, we can get the reusable cell and add the cell
to the tableView
. And at the second one, we just obtain another reusable cell. Finally, the method returns the cell obtained from the second line. The returned cell is different with the cell being having added to the tableView already.
In this case, the cell added to tableView at first line is just replaced by the returned cell at the final line?
Thanks.
回答1:
After having a look at the official documentation I realised that your interpretation is somewhat correct.
Could you please have a look at this answer, as this might bring up more clarity.
回答2:
Method dequeueReusableCell(withIdentifier:)
is older than dequeueReusableCell(withIdentifier:for:)
, and the main difference between them is that first will return nil, if cell don't registered, the second will throw an exception, and an app will crash. IndexPath requires for height calculation(if defined tableView:heightForRowAtIndexPath
)
回答3:
dequeueReusableCell(withIdentifier:)
AND dequeueReusableCell(withIdentifier:for:)
Both returning the cell, but older method return nil while latest method crashes the app.
older may support upto iOS 5 while newer method support iOS 6 and Above
回答4:
If you will use below method you always get the initialized instance and it will always be the right size for that indexpath, so you will be able to do layout inside your contentview knowing that the size is correct. As this will set the cell size before returning it that's why we need to add indexPath
.
let cell: UITableViewCell = self.tableView.dequeueReusableCell(with:
"Cell", for: indexPath)
In below case you have to check if the cell is nil, configure it yourself.
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "Cell")
if (cell == nil) {
cell = UITableViewCell(style:UITableViewCellStyle.subtitle, reuseIdentifier:"Cell")
}
Note: In newer version i.e. self.tableView.dequeueReusableCell(with:
"Cell", for: indexPath)
app crashes if you didn't register a class/nib for the identifier. In older version i.e. tableView.dequeueReusableCell(withIdentifier: "Cell")
version returns nil in that case. Moreover if you are using storyboard
you don't need to worry about registering the cell.
dequeueReusableCellWithIdentifier:forIndexPath:
will always return a cell. On the other hand dequeueReusableCellWithIdentifier:
will return nil if no reusable cell that's why nil check is required.
来源:https://stackoverflow.com/questions/44213804/ios-what-is-a-difference-between-dequeuereusablecellwithidentifierfor-and-d