【问题标题】:dequeueReusableCellWithIdentifier returns nildequeueReusableCellWithIdentifier 返回 nil
【发布时间】:2014-09-27 14:41:06
【问题描述】:

我正在使用 Storyboard 的原型 UITableViewCell 并在 cellForRowAtIndexPath 中调用 dequeueReusableCellWithIdentifier 时得到 nil。我已经三次检查原型 tableviewcell 的 Xcode 标识符是“PersonCell”,删除原型单元格并再次添加它,注释掉 UITableViewController 的 UISearchDisplyDelegate 和 UISearchBarDelegate 继承,但仍然为零。我难住了。有人遇到过这种情况吗?

#import "PeopleGroupPickerViewController.h"
#import "DAL.h"
#import "Person.h"

@interface PeopleGroupPickerViewController ()

@end

@implementation PeopleGroupPickerViewController
{
 NSArray *people;
 NSArray *searchResults;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 people = [[DAL sharedInstance] getPeople:false];
 [self.tableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:@"PersonCell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
 NSString *lastNameSearch = searchText.lowercaseString;
 NSString *firstNameSearch = @"";
 if ([lastNameSearch rangeOfString:@","].length > 0)
 {
  NSArray *names = [lastNameSearch componentsSeparatedByString:@","];
  //NSLog(@"names count",names.count);
  if(names.count > 1)
  {
   lastNameSearch = names[0];
   firstNameSearch = names[1];
   //NSLog(@"first %@ last %@",firstNameSearch,lastNameSearch);
  }
 }

 NSMutableString *predicateText = [[NSMutableString alloc] initWithFormat:@"(sLastName contains[c] '%@')",lastNameSearch];
 if(![firstNameSearch isEqualToString:@""])
 {
  [predicateText appendFormat:@" AND (sFirstName contains[c] '%@')",firstNameSearch];
 }
 NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:predicateText.copy];
 searchResults = [people filteredArrayUsingPredicate:resultPredicate];
 NSLog(@"filterContentForSearchText, searching for %@, # results: %d",predicateText, searchResults.count);
}

-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
 [self filterContentForSearchText:searchString scope:nil];
 return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 if(tableView == self.searchDisplayController.searchResultsTableView)
 {
  return 1;
 }
 else
 {
  return 1;
 }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 if(tableView == self.searchDisplayController.searchResultsTableView)
 {
  return searchResults.count;
 }
 else
 {
  return people.count;
 }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

 Person_ *thisPerson;
 if(tableView == self.searchDisplayController.searchResultsTableView)
 {
  thisPerson = (Person_ *) searchResults[indexPath.row];
 }
 else
 {
  thisPerson = (Person_ *) people[indexPath.row];
 }
 Person_ *thisSpouse = [[DAL sharedInstance] getSpouse:thisPerson People:people];

 cell.fieldName.text = thisPerson.sName;
 cell.fieldValue.text = thisSpouse.sName;

 return cell;
}

【问题讨论】:

  • dequeueReusableCellWithIdentifier: 获取nil 是完全正常和预期的。如果是nil,则必须创建单元实例。如果您注册了一个单元格,请使用dequeueReusableCellWithIdentifier:forIndexPath:。那不会返回nil
  • rmaddy,您提到的问题是“对于具有原型单元格的情节提要和表格视图,[tableView dequeueReusableCellWithIdentifier:] 不应返回 nil。”。该问题的问题与应用程序委托重新初始化 tableviewcontroller 有关。我在情节提要中使用了一个普通的 UITableViewController,所以我不确定这是同一个问题。
  • @rmaddy 你是最棒的!

标签: ios objective-c uitableview xcode-storyboard


【解决方案1】:

就我而言,问题出在此处:

我认为应该在身份检查器中设置恢复ID

而应该在属性检查器中设置标识符

我的代码最终变成了:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TrueOne") as! TrueOneCell
cell.setup(card: cards[indexPath.row])
return cell

并且不需要来创建实例。如果它返回 nil,则问题可能来自其他地方。

【讨论】:

    【解决方案2】:

    如果容纳您的单元格的tableView 没有将您的视图控制器也分配为表格视图的委托和数据源,则可能会发生这种情况。

    【讨论】:

      【解决方案3】:

      添加到@Fonix 答案

      确保Inherit Module From Target 已启用

      【讨论】:

        【解决方案4】:

        在身份检查器中设置“恢复 ID”后,我遇到了这个问题。设置重用标识符的正确位置是属性检查器中的“标识符”字段。

        【讨论】:

          【解决方案5】:

          正如 maddy 在 cmets 中所说,如果单元格为 nil,您应该自己创建单元格,或者在您的 viewDidLoad 中或在适当的情况下,您可以调用其中一种方法来让 tableview 为您创建单元格

          Objective-c

          [self.tableView registerClass:<#(__unsafe_unretained Class)#> forCellReuseIdentifier:<#(NSString *)#>]
          [self.tableView registerNib:<#(UINib *)#> forCellReuseIdentifier:<#(NSString *)#>]
          

          斯威夫特

          tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "CellID1")
          tableView.register(UINib(nibName: "yourNibName", bundle: nil), forCellReuseIdentifier: "CellID2")
          

          【讨论】:

          • [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"PeopleCell"];越过 nil 错误并绑定表,但它绑定了错误的单元格类。原型单元格样式是“Right Detail”,但它绑定了我无法绑定 detailTextLabel 的通用 UITableViewCell 类。我通过创建自己的自定义 UITableViewCell 子类并将标签连接到其插座来解决此问题。
          • 另外注意:我必须在 viewDidLoad 中为常规 UITableView (self.tableView) 和搜索结果 UITableView (self.searchDisplayController.searchResultsTableView) 注册自定义 UITableViewCell 子类。
          • 刚刚完成了类似情况的工作。将实例化 TableViewController 的代码添加到父 UIViewController 对象,然后将我的笔尖中的 UITableView 附加到我刚刚实例化的 UITableViewController。这是我发现的技巧,#1) 创建 UITableViewController 并将其添加到您的父视图控制器。 2) 然后找到容器视图中的子 UITableView 并使用“[childTableVC setTableView:myTableView]”。 3)然后将单元格注册到表格视图中。在添加 tableViewController 之前注册单元格时对我不起作用。
          猜你喜欢
          • 1970-01-01
          • 2015-03-06
          • 1970-01-01
          • 1970-01-01
          • 2014-01-17
          • 1970-01-01
          • 1970-01-01
          • 2012-04-17
          • 2012-11-02
          相关资源
          最近更新 更多