dequeueReusableCellWithIdentifier always return non nil?

时光总嘲笑我的痴心妄想 提交于 2019-12-19 04:07:34

问题


In the classic pattern below the returned cell from dequeueReusableCellWithIdentifier is always non nil. Why? Shouldn't we have to allocated some cell first then we can have some to be reused?

I am using a customize cell and it was created in the storyboard. (However, if I had used the default UITableViewCell ,the phenomena is same - the returned cell is still always non-nil).

Enviroment: Xcode 4.3.3 iOs 5.1

AlbumListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell"];

//cell always non nil --- why??
if(cell == nil){
    cell = [[AlbumListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AlbumCell"];

}

回答1:


If you're using storyboard and set the UITableViewCell's Identifier to the identifier to the one you are using in dequeueReusableCellWithIdentifier ("AlbumCell" in your case) in the storyboard file, the UITableView will always create cells for you. I guess this is a feature of storyboard. If the identifier can't be found in your storyboard, then you need to create cells manually.




回答2:


I know it is possible for the return of the cell to be nil and you should check for it. Usually this will not be the case as you should get a cell created for you from your identifier you supplied. If you for instance did not have the identifier correct and the pool was empty I believe that is the case where you would get nil and you could do an aloc init to create a new cell. Also I should add the prototype cell you create in xcode is what creates the cell and that is why it should start as non nil and you don't have to create one.

To better answer this go to iTunes and go to the course iPad and iPhone Application Development - Paul Hegarty - Stanford CS193p course. Go to lecture (25) 9. Table Views go to 26 minutes in.



来源:https://stackoverflow.com/questions/11788197/dequeuereusablecellwithidentifier-always-return-non-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!