为什么不如果(cell == nil)工作?

我试图使用几个不同的重用标识符,让用户能够input数据没有segue …(只是认为这将是很好的)。

这是我第一次尝试:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(addC && indexPath.row == 0 && indexPath.section == 0){ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; UITextField *txtField = [[UITexField alloc] initWithFrame:CGRectMake(10, 7, 260, 30); [cell addsubview:txtField]; cell.txtField.delegate = self; cell.txtField.text = nil; return cell; 

但是,我注意到,如果我不止一次地使用该字段,那么文本字段将会重复,而在下面留下旧的值。 所以经过一番研究,我补充道:

 if(cell == nil){ } 

围绕txtField初始化,但不会触发。 我结束了解决这个问题完全通过制作一个单独的UITableViewCell类,但现在我正在尝试添加SWTableViewCell ,并将我的单元格从UITableViewCell更改为CustomTableViewCell。

这个自定义实用程序插件到我的表和初始化需要你使用

 if (cell == nil) { NSMutableArray *leftUtilityButtons = [NSMutableArray new]; NSMutableArray *rightUtilityButtons = [NSMutableArray new]; [leftUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0] icon:[UIImage imageNamed:@"check.png"]]; [leftUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0] icon:[UIImage imageNamed:@"clock.png"]]; [leftUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0] icon:[UIImage imageNamed:@"cross.png"]]; [leftUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0] icon:[UIImage imageNamed:@"list.png"]]; [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] title:@"More"]; [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"Delete"]; cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier containingTableView:_tableView // For row height and selection leftUtilityButtons:leftUtilityButtons rightUtilityButtons:rightUtilityButtons]; cell.delegate = self; } 

如果还没有实现这个插件,但之前,当我正在研究我的问题,现在我还没有find任何东西。

我也试过了

 if (cell.contentView == nil) if (cell.contentView.subViews == nil) if (cell.subViews == nil) 

但由于某种原因,它不会像第一次那样开火。 我究竟做错了什么?

你的第一个问题的答案(为什么cell == nil永远不会被调用):

dequeueReusableCellWithIdentifier:forIndexPath:dequeueReusableCellWithIdentifier:的文档声明:

如果您为指定的标识符注册了一个类,并且必须创build一个新的单元格,则此方法通过调用其initWithStyle:reuseIdentifier:方法来初始化该单元格。 对于基于笔尖的单元格,此方法从提供的nib文件加载单元格对象。 如果现有的单元可用于重用,则此方法将调用单元的prepareForReuse方法。

这是说,如果你使用registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:为了注册重用标识符,它出现,否则你的单元格将为零,因为你从来没有手动实例化一个,你永远不会有机会自己实例化一个。

有几种方法可以解决这个问题,但最好和最有效的方法是创build一个UITableViewCell子类,并在您的子类中添加您想要的任何自定义子视图,如果您正在使用子视图,并且每次重用时都要设置子视图的属性。

所以基本上只是将所有的自定义子视图添加和初始化代码移动到initWithStyle:reuseIdentifier:

然后你的tableView:cellForRowAtIndexPath:方法将被重写为类似于:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(addC && indexPath.row == 0 && indexPath.section == 0) { MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; cell.txtField.delegate = self; cell.txtField.text = nil; return cell; } } 

如果cell nil则表格视图中没有单元格。 所以你的txtField除了txtField都不会起作用。

听起来你想要寻找的是一个单元格已经有一个文本字段? 所以cell 显然不会nil

大多数程序使用UITableViewCell的自定义子类,以使子视图的生命周期自然匹配单元本身。 在你的情况下:

  1. 你应该添加到cell.contentView ,而不是直接的cell ;
  2. 那么你可以通过检查[cell.contentView.subviews count]来判断是否添加了任何内容,因为内容视图的点在于它与表视图单元格可能自己添加或删除的任何其他子视图不同一个普通的操作部分。