【发布时间】: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