UITableViewCell 不等于 null

编程入门 行业动态 更新时间:2024-10-23 03:20:28
本文介绍了UITableViewCell 不等于 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用下面的代码时遇到了问题,它基本上是从故事板实例化扩展 uitableviewcell.我遇到的问题是 leftMenuCell 似乎 never 等于 null,因此永远不会进入启动块.我做错了什么?

I'm having trouble with the following code below, which basically is instantiating an extend uitableviewcell from a storyboard. The problem I'm having is that it seems leftMenuCell is never equal to null, and thus never enters the initiating block. What am I doing wrong?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"LeftMenuCell"; MenuCell *leftMenuCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(leftMenuCell == nil) { NSLog(@"creating a new cell"); leftMenuCell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } ....

推荐答案

您没有做错任何事情,这只是在故事板中制作单元格时表格视图的工作方式.方法 dequeueReusableCellWithIdentifier:,当单元格在故事板的表格视图中时,总是返回一个有效的单元格.好像很多程序员都没搞清楚,还是把if cell==nil子句加进去了.这是来自文档:

You're not doing anything wrong, that's just the way table views work when you make the cell in the storyboard. The method dequeueReusableCellWithIdentifier:, always returns a valid cell when that cell is in a table view in a storyboard. It seems that many programmers haven't figured this out, and still include the if cell==nil clause. This is from the docs:

"如果 dequeueReusableCellWithIdentifier: 方法要求一个在故事板中定义的单元格,该方法总是返回一个有效的单元格.如果没有等待被重用的回收单元格,该方法使用中的信息创建一个新的单元格故事板本身.这消除了检查 nil 的返回值和手动创建单元格的需要"

"If the dequeueReusableCellWithIdentifier: method asks for a cell that’s defined in a storyboard, the method always returns a valid cell. If there is not a recycled cell waiting to be reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually"

更多推荐

UITableViewCell 不等于 null

本文发布于:2023-11-15 21:37:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不等于   UITableViewCell   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!