Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios dequeueReusableCellWithIdentifier返回nil_Ios_Objective C_Uitableview_Xcode Storyboard - Fatal编程技术网

Ios dequeueReusableCellWithIdentifier返回nil

Ios dequeueReusableCellWithIdentifier返回nil,ios,objective-c,uitableview,xcode-storyboard,Ios,Objective C,Uitableview,Xcode Storyboard,我使用的是故事板的原型UITableViewCell,当在cellForRowAtIndexPath中调用dequeueReusableCellWithIdentifier时,结果为零。我已经三次检查了prototype tableviewcell的Xcode标识符是否为“PersonCell”,删除了prototype单元格并再次添加,注释掉了UITableViewController的UISearchDisplayDelegate和UISearchBarDelegate继承,但仍然为零。我被

我使用的是故事板的原型UITableViewCell,当在cellForRowAtIndexPath中调用dequeueReusableCellWithIdentifier时,结果为零。我已经三次检查了prototype tableviewcell的Xcode标识符是否为“PersonCell”,删除了prototype单元格并再次添加,注释掉了UITableViewController的UISearchDisplayDelegate和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;
}

如maddy在评论中所述,如果单元格为nil,您应该自己创建单元格,或者在
视图didload
中,或者在适当的情况下,您可以调用这些方法之一,让tableview为您创建单元格

目标-c

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

在identity inspector中设置“Restoration ID”后,我遇到了这个问题。设置reuseIdentifier的正确位置是属性检查器中的“标识符”字段。

添加到@Fonix答案中

确保启用了从目标继承模块


如果容纳单元格的
tableView
没有将viewcontroller指定为tableView的代理和数据源,则可能发生这种情况


dequeueReusableCellWithIdentifier:
获取
nil
是完全正常的,也是意料之中的。如果是
nil
,则必须创建单元格实例。如果您注册了一个单元格,请使用
dequeueReusableCellWithIdentifier:forIndexPath:
。这不会返回
nil
.rmaddy,您提到的问题是“对于具有原型单元的故事板和tableView,[tableView dequeueReusableCellWithIdentifier:]不应返回nil。”。该问题与应用程序委托重新初始化tableviewcontroller有关。我在故事板中使用的是普通的UITableViewController,所以我不确定这是否是同一个问题。@rmaddy你是最好的![self.tableView注册表类:[UITableViewCell类]强制重用标识符:@“PeopleCell”];通过nil错误并绑定表,但它绑定了错误的单元格类。原型单元样式是“Right Detail”,但它绑定了一个通用的UITableViewCell类,我无法绑定detailTextLabel。我通过创建自己的自定义UITableViewCell子类并将标签连接到其插座来解决此问题。另一个注意事项是:我必须为常规UITableView(self.tableView)和搜索结果UITableView(self.searchDisplayController.searchResultsTableView)注册自定义UITableViewCell子类在viewDidLoad中。刚刚完成类似情况下的工作。。将实例化的TableViewController代码添加到父UIViewController对象,然后将nib中的UITableView附加到刚才实例化的UITableViewController。以下是我发现的技巧,#1)创建UITableViewController并将其添加到父视图控制器。2) 然后找到容器视图中的子UITableView并使用“[childTableVC setTableView:myTableView]”。3) 然后将单元格注册到表视图中。在添加tableViewController之前注册单元格时不适用于我。
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "CellID1")
tableView.register(UINib(nibName: "yourNibName", bundle: nil), forCellReuseIdentifier: "CellID2")