【问题标题】:dequeueReusableCellWithIdentifier returns nil using storyboard static cellsdequeueReusableCellWithIdentifier 使用情节提要静态单元格返回 nil
【发布时间】:2011-12-29 04:48:14
【问题描述】:

我玩得很开心。使用情节提要,我创建了一个带有静态单元格的表格视图控制器,其中包含一个 UITextField 以允许用户输入。当用户完成后,我想检索文本字段的内容。

这是我所做的:

  • 创建了一个名为 SingleLineFieldTableViewCellUITableViewCell 子类
  • IBOutlet UITextField *textField; 添加到子类中,并将其声明为属性(非原子、保留)并合成它。
  • IBOutlet SingleLineFieldTableViewCell *cellNamed;添加到拥有的表视图控制器中,并将其声明为属性(非原子,保留)并合成它。

  • 在情节提要中,我有一个带有静态单元格的表格视图控制器。其中一个单元格是自定义单元格,它被声明为SingleLineFieldTableViewCell 并拥有UITextField。它还被分配了一个小区标识符。

  • 我将表格视图单元格的引用出口和文本字段附加到上面列出的相应 IBOutlets。

当我运行时,dequeueReusableCellWithIdentifier 返回nil。我认为使用 Xcode 4 和情节提要 dequeueReusableCellWithIdentifier,根据 Converting to Storyboards Release Notes,“dequeueReusableCellWithIdentifier: 方法保证返回一个单元格(前提是您已经定义了一个具有给定标识符的单元格)”。

奇怪的是,当我在模拟器中运行时,表格按预期显示(部分、单元格大小等),只是我无法编辑自定义单元格。

我很茫然。有什么帮助或想法吗?

--约翰

【问题讨论】:

    标签: iphone ios xcode uitableview storyboard


    【解决方案1】:

    我知道这是一年前的问题,但我今天自己刚刚解决了这个问题。

    我认为这里的问题是使用静态单元格。

    请参阅 Apple 文档:http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

    基本上,如果您使用的是静态单元格,则无需使用

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    数据源方法,而只是生成所有 UI 元素(在静态单元格内)的出口并直接设置它们。

    如果您需要使用此方法,则必须将表格更改为动态内容模式。

    【讨论】:

      【解决方案2】:

      您是为 iOS 5 还是 4 构建?

      如果您尝试使用 4.x,它不会崩溃,因为该方法有效,但不会返回单元格。我用自定义类设置它没有问题。这是我的整个方法:

      -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
          GameDetailCell* cell=[tableView dequeueReusableCellWithIdentifier:@"gameCell"];
          [self configureCell:cell atIndexPath:indexPath];
          return cell;
      }
      

      我的故事板看起来像:

      【讨论】:

      • 我在 iOS 5 中使用它。我想知道它是否与单元格对象在情节提要中的表示方式有关。如果创建动态单元格,则层次结构为表格视图 -> 单元格。在带有部分的静态单元格中,层次结构是表格视图 -> 部分 -> 单元格。我想知道这种封装是否在使用静态单元格时隐藏了表格视图中的标识符,但在使用动态单元格时却没有。顺便说一句,双端队列正在为我使用动态单元格。想法?
      • 你是继承 UITableviewController 还是 UIViewController?
      • 是的,我有一个从 UITableViewController 派生的自定义表格视图控制器。它将此类分配给情节提要中的控制器。
      • 你们找到解决方案了吗?我的和上面的差不多,但是我的手机没有被使用。
      【解决方案3】:

      我也是一个新手,所以这可能完全是废话,但我会告诉UITextLabel 在用户完成编辑时调用我的方法之一,而不用担心尝试从表视图中删除它:

      - (IBAction)userFinishedEditing:(id)sender
      {
          ...
      }
      
      - (void) someMethod
      {
          ...
      
          UITextLabel *label = ...;
          [label addTarget:self action:@selector(userFinishedEditing:sender:) forControlEvents: 
      UIControlEventEditingDidEnd];
      
          ...
      }
      

      【讨论】:

        【解决方案4】:

        根据 Apple 的文档(使用数据填充静态表视图)http://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW31

        注意:如果故事板中的表格视图是静态的,则自定义子类 的UITableViewController 包含表视图不应该 实现数据源协议。相反,表视图控制器 应该使用它的viewDidLoad 方法来填充表格视图的数据。

        因此,您只需从 View Controller 中删除所有 Table View 数据源方法。


        可选:

        但是,如果您的 View Controller 也是其他动态 Table View 的数据源并且您仍然需要这些方法,则可以只调用 super 的静态 Table View 对应的数据源方法:

        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {
            // Static Table View
            if (tableView == self.tableView)
                return [super numberOfSectionsInTableView:tableView];
        
            // Dynamic Table View
            // ...
        }
        
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            // Static Table View
            if (tableView == self.tableView)
                return [super tableView:tableView numberOfRowsInSection:section];
        
            // Dynamic Table View
            // ...
        }
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            // Static Table View
            if (tableView == self.tableView)
                return [super tableView:tableView cellForRowAtIndexPath:indexPath];
        
            // Dynamic Table View
            // ...
        }
        

        【讨论】:

          【解决方案5】:
              Alert.m Class in which we used custom cell..
          
              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          
                  static NSString *CellIdentifier = @"mycell";
                  AlertCustomCell *cell = (AlertCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                  if (cell == nil) 
                  {   
                      cell=[[[AlertCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                  }
          
          
              cell.lAlert.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"alertName"];
                  cell.lblDate.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"date"];
          
          
                  UIImageView*imgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)];
                  imgview.image=[UIImage imageNamed:@"strip_s14.png" ];
                  UIImageView*selimgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)];
                  selimgview.image=[UIImage imageNamed:@"strip_s14_h.png" ];
                  [cell setSelectedBackgroundView:selimgview];
                  cell.backgroundView = imgview;
          
                  cell.backgroundColor=[UIColor clearColor];
          
                  return cell;
              }
          
          AlertCustomCell.m
          
          #import "AlertCustomCell.h"
          
          
          @implementation AlertCustomCell
          @synthesize lblAlert,lblDate;
          
          - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
          
              self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
              if (self) {
                  Alert=[[UITextField alloc]initWithFrame:CGRectMake(10, 18, 80, 21)];
                  Alert.backgroundColor=[UIColor clearColor];
                  Alert.text=@"Alert 1";
                  Alert.font=[UIFont fontWithName:@"Arial-BoldMT" size:15.0];
                  [self.contentView addSubview:lblAlert];
          
                  lblDate=[[UILabel alloc]initWithFrame:CGRectMake(70, 18, 150, 21)];
                  lblDate.backgroundColor=[UIColor clearColor];
                  lblDate.text=@"july 12,2011 4:17 PM";
                  lblDate.font=[UIFont fontWithName:@"ArialMT" size:15.0];
                  [self.contentView addSubview:lblDate];
              }
              return self;
          }
          

          【讨论】:

          • 感谢您的代码。这类似于我在 iOS 5 和 XCode4 之前编写的代码。但我正在尝试使用故事板并在其中创建单元格来实现这一点(尽可能远离所有设计源代码)。
          • 发布通用代码而不解释它与问题的关系不是很有帮助。此外,代码可能相关,因为它看起来早于故事板。
          猜你喜欢
          • 2012-04-17
          • 2012-11-02
          • 2014-09-27
          • 2016-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-20
          • 1970-01-01
          相关资源
          最近更新 更多