4 行の UITableView がある場合、そのビューに移動するときに cellForRowAtIndexPath メソッドを 4 回呼び出すべきではありませんか? 複数回呼び出されていることがわかりました。たとえば、4行ある場合、UITableビューにドリルダウンすると8回呼び出され、同じUITableViewに戻ると12回呼び出されます。
この方法がどのように機能するかを誤解しているだけですか?表示される行ごとに1回呼び出す必要があると思いました(テーブルビューの内容がiPad画面に簡単に収まるため、スクロールは必要ありません)。
おそらく、UITableView は iPad 上の UISplitViewController の MasterViewController に含まれていることに注意してください。
編集:私が得ている本当の問題は、次のコードが適用されている方法に一貫性がないことです:
if (selectedNavItem.folder.isAssignedToUser != [NSNumber numberWithInt:1])
{
NSLog(@"%@", cell.textLabel.text);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.enabled = NO;
cell.detailTextLabel.text = @"Access not granted";
cell.detailTextLabel.enabled = NO;
}
ナビゲーション スタックの特定のレベルに初めて "ドリルダウン" すると、正常に機能します。しかし、後で上下に移動すると、さまざまな行が正しく処理されないことがわかります。つまり、有効にする必要があるときに無効になってしまいます。そのため、何が起こっているのかを調べるために、ブレーク ポイントといくつかのログ コードを挿入しました。上記のコードが行数よりも多く呼び出されていることがわかりました-そして、ログに記録していた cell.textLabel.text 値は意味がありません-同じ行の値が複数回ログに記録されていました-そして他の行はそうではありませんでしたまったくログに記録されています。
編集:要求に応じていくつかのコードを提供します:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FolderCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"NavItem" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort keys as appropriate.
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.
managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
NavItem *selectedNavItem = (NavItem *)managedObject;
cell.textLabel.text = [[managedObject valueForKey:@"name"] description];
cell.detailTextLabel.text = @"";
if (selectedNavItem.folder != nil) {
cell.imageView.image = [UIImage imageNamed:@"Folder.png"];
//NSLog(@"%@", selectedNavItem.folder.isAssignedToUser);
if (selectedNavItem.folder.isAssignedToUser != [NSNumber numberWithInt:1])
{
NSLog(@"%@", cell.textLabel.text);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.enabled = NO;
cell.detailTextLabel.text = @"Access not granted";
cell.detailTextLabel.enabled = NO;
}
}
else if (selectedNavItem.document != nil) {
cell.detailTextLabel.text = [[selectedNavItem.document valueForKey:@"itemDescription"] description];
if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"pdf"])
{
cell.imageView.image = [UIImage imageNamed:@"pdf.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"doc"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"docx"])
{
cell.imageView.image = [UIImage imageNamed:@"Word-32x32.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"xls"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"xlsx"])
{
cell.imageView.image = [UIImage imageNamed:@"Excel-32x32.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"ppt"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"pps"])
{
cell.imageView.image = [UIImage imageNamed:@"Powerpoint-32x32.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"mp3"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"wav"])
{
cell.imageView.image = [UIImage imageNamed:@"65-note.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"mp4"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"mov"])
{
cell.imageView.image = [UIImage imageNamed:@"46-movie-2.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"ibooks"])
{
cell.imageView.image = [UIImage imageNamed:@"ibooks-icon.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"png"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"bmp"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"jpg"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"jpeg"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"tif"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"tiff"] || [[selectedNavItem.document.fileName pathExtension] isEqualToString:@"gif"])
{
cell.imageView.image = [UIImage imageNamed:@"41-picture-frame.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:@"179-notepad.png"];
}
}
else if (selectedNavItem.attachment != nil) {
cell.detailTextLabel.text = [[selectedNavItem.attachment valueForKey:@"itemDescription"] description];
if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"pdf"])
{
cell.imageView.image = [UIImage imageNamed:@"pdf.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"doc"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"docx"])
{
cell.imageView.image = [UIImage imageNamed:@"Word-32x32.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"xls"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"xlsx"])
{
cell.imageView.image = [UIImage imageNamed:@"Excel-32x32.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"ppt"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"pps"])
{
cell.imageView.image = [UIImage imageNamed:@"Powerpoint-32x32.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"mp3"])
{
cell.imageView.image = [UIImage imageNamed:@"65-note.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"mp4"])
{
cell.imageView.image = [UIImage imageNamed:@"46-movie-2.png"];
}
else if ([[selectedNavItem.document.fileName pathExtension] isEqualToString:@"ibooks"])
{
cell.imageView.image = [UIImage imageNamed:@"ibooks-icon.png"];
}
else if ([[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"png"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"bmp"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"jpg"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"jpeg"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"tif"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"tiff"] || [[selectedNavItem.attachment.fileName pathExtension] isEqualToString:@"gif"])
{
cell.imageView.image = [UIImage imageNamed:@"41-picture-frame.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:@"179-notepad.png"];
}
}
}