UITableViewCell 不等于 null [英] UITableViewCell not equal to null

查看:50
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆