我正在使用Storyboard的原型UITableViewCell,并在cellForRowAtIndexPath中调用dequeueReusableCellWithIdentifier时获取nil。我已经三次检查原型tableviewcell的Xcode标识符是" PersonCell",删除原型单元并再次添加它,注释掉UITearchDisplyDelegate和UISearchBarDelegate继承UITableViewController,仍然得到nil 。我很难过。有没有人碰到这个?
#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;
}
答案 0 :(得分:18)
正如Maddy在评论中所述,你应该自己创建单元格,如果它是零,或者在你的viewDidLoad
中或在适当的情况下,你可以调用其中一种方法让tableview为你创建单元格< / p>
<强>目标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")
答案 1 :(得分:2)
在设置&#34;恢复ID&#34;之后我遇到了这个问题。在身份检查员。设置reuseIdentifier的正确位置是&#34;标识符&#34;在“属性”检查器中找到的字段。
答案 2 :(得分:0)
答案 3 :(得分:0)