在阅读了几十个类似的问题后,我想从这句话开始:"我DID设置了NSFetchedResultsController的委托,它没有用."
所以我有一个简单的TableViewController,其单元格用NSFetchedResultsController填充.这是FRC初始化代码:
@property (strong, nonatomic) NSFetchedResultsController *frc;
...
- (NSFetchedResultsController *)frc
{
if (!_frc)
{
NSError *error = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:e_product];
request.sortDescriptors = [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"product_group.product_group_name" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:f_product_name ascending:YES],
nil];
_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[DTGGlobalSettings sharedInstance].moc sectionNameKeyPath:@"product_group.product_group_name" cacheName:nil];
_frc.delegate = self;
[_frc performFetch:&error];
if (error)
{
NSLog(@"Error while fetching products: %@!", error.userInfo);
}
}
return _frc;
}
Run Code Online (Sandbox Code Playgroud)
我还使用一些调试标签实现了NSFetchedResultsController委托方法:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, …Run Code Online (Sandbox Code Playgroud) 我正在从定制我的TableViewCells迁移tableView:cellForRow:atIndexPath:到使用自定义UITableViewCell子类.这是我如何做到的:
首先,创建空的XIB,拖到UITableViewCell那里然后放在UILabel上面.创建了一个类(子类UITableViewCell),并在Interface Builder的属性编辑器中将类设置为MyCell.然后,在我的TableViewController中,输入以下内容:
- (void)viewDidLoad
{
[super viewDidLoad];
// load custom cell
UINib *cellNIB = [UINib nibWithNibName:@"MyCell" bundle:nil];
if (cellNIB)
{
[self.tableView registerNib:cellNIB forCellReuseIdentifier:@"MyCell"];
} else NSLog(@"failed to load nib");
}
Run Code Online (Sandbox Code Playgroud)
之后,我清除了所有自定义代码,tableView:cellForRow:atIndexPath:只留下默认行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我希望在每个单元格中看到一堆单个标签的单元格(我在创建XIB时在中间放置的标签).但相反,我只看到纯白色的空单元格,并添加/删除组件到XIB布局没有帮助.
我花了一天尝试不同的选项,比如在Interface Builder中为自定义单元格XIB设置reuseIdentifier并加载视图tableView:cellForRow:atIndexPath:,但没有任何帮助.