2014-03-26 95 views
0

我试图使用几个不同的重用标识符,让用户能够输入数据没有segue ...(只是觉得它会很好)。为什么没有(cell == nil)工作?

这是我第一次尝试:

- (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; 

不过,我注意到,如果我使用的字段不止一次文本字段会重复,留下旧值下方。 因此,一些reasearch后,我说:

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; 
} 

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

我也曾尝试

if (cell.contentView == nil) 

if (cell.contentView.subViews == nil) 

if (cell.subViews == nil) 

,但由于某种原因,将不会触发第一次像它应该。我究竟做错了什么?

回答

1

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

dequeueReusableCellWithIdentifier:forIndexPath:dequeueReusableCellWithIdentifier:的文档指出:

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

这是什么意思是,如果你为了注册重用标识,看来你没有,否则你的电池将是零,因为你永远不手动实例化一个使用registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:,你将永远不会有机会自己实例化一个。

有几种方法可以解决这个问题,但最好和最有效的方法是创建一个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; 
    } 
} 
+0

如果我可以投票你,我会多数民众赞成正是我是如何解决我的第一个问题,但接着下一个,如果你看看他使用的SWTableViewCell if(cell == nil)复制那个。因为它不会被称为ID必须翻拍他的整个项目几乎使用它? theres暂时没有解决方法 – Snymax

+0

只需将代码放入'initWithStyle:reuseIdentifier:'我将编辑我的答案并举例说明如何工作。 – Dima

+0

试图模拟这个后,它看起来像默认的'SWTableViewCell'初始值设定程序期望引用'UITableView',这是不容易从该点访问。因此,我会建议摆脱注册的单元格,而是结构化代码以允许手动初始化单元格,就像他们的文档中的代码示例一样。 – Dima

0

如果cellnil那么你有没有小区从表视图回来。所以你的txtField除了无论如何都无效。

这听起来像你想寻找的是让已有的文本字段的细胞回?所以cell明确不nil

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

  1. 你应该加入cell.contentView,而不是cell直接;
  2. ,那么你或许可以告诉你是否已经通过检查[cell.contentView.subviews count]作为内容视图的点添加任何东西是它不同于任何其他子视图该表视图的细胞可以作为一个普通的一部分从自己添加或删除操作。
+0

你的代码仍然触发间隔重用被加载,但是我明白为什么现在虽然时间。虽然它确实阻止了加倍,所以谢谢 – Snymax

相关问题