質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Q&A

1回答

2552閲覧

fetchedResultsControllerで検索結果の取得/表示方法

退会済みユーザー

退会済みユーザー

総合スコア0

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

0グッド

1クリップ

投稿2014/09/25 14:46

CoreData(Magical Record使用)からフェッチしたテーブル一覧画面で検索を行い、
検索結果を表示させたいのですがビルドが通りません。

-(NSFetchedResultsController*)fetchedResultsController:でfindALL用と検索用で分けているのですが、
以下で「-[__NSArrayM sections]: unrecognized selector sent to instance 0x7a7ddcb0」という
エラーが発生してしまいます。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

検索結果を取得/表示する方法を教えて頂けませんでしょうか。
以下、現状のコードです。

lang

1#import "FirstViewController.h" 2#import "CustomTableViewCell.h" 3#import "TableViewConst.h" 4#import "DetailViewController.h" 5#import "Person.h" 6 7@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate, NSFetchedResultsControllerDelegate> 8 9@property (strong, nonatomic) IBOutlet UITableView *tableView; 10@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; 11 12@end 13 14@implementation FirstViewController 15 16- (void)viewDidLoad 17{ 18 [super viewDidLoad]; 19 UINib *nib = [UINib nibWithNibName:TableViewCustomCellIdentifier bundle:nil]; 20 [self.tableView registerNib:nib forCellReuseIdentifier:@"Cell"]; 21} 22 23- (NSFetchedResultsController*)fetchedResultsController 24{ 25 //検索要 26 if([self.searchBar.text length] > 0){ 27 self.fetchedResultsController = [[Person MR_findAllSortedBy:@"name" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"name contains[c] %@",self.searchBar.text] inContext:[NSManagedObjectContext MR_defaultContext]] mutableCopy]; 28 }//findAll用 29 else if(_fetchedResultsController == nil){ 30 self.fetchedResultsController = [Person MR_fetchAllSortedBy:@"name" ascending:YES withPredicate:nil groupBy:nil delegate:self]; 31 } 32 return _fetchedResultsController; 33} 34 35- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 36{ 37 NSInteger count = [[self.fetchedResultsController sections] count]; 38 if (count == 0) { 39 count = 1; 40 } 41 return count; 42} 43 44- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 45{ 46 return @"一覧"; 47} 48 49- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 50{ 51 NSInteger numberOfRows = 0; 52 53 if ([[self.fetchedResultsController sections] count] > 0) { 54 id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section]; 55 numberOfRows = [sectionInfo numberOfObjects]; 56 } 57 58 return numberOfRows; 59} 60 61- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 62{ 63 static NSString *CellIdentifier = @"Cell"; 64 CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 65 66 if (cell == nil) { 67 cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 68 } 69 70 [self configureCell:cell atIndexPath:indexPath]; 71 72 return cell; 73} 74 75- (void)configureCell:(CustomTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 76 Person *person = (Person *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 77 cell.person = person; 78} 79 80- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 81 UITableView *tableView = self.tableView; 82 83 switch(type) { 84 case NSFetchedResultsChangeInsert: 85 [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 86 break; 87 88 case NSFetchedResultsChangeDelete: 89 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 90 break; 91 92 case NSFetchedResultsChangeUpdate: 93 [self configureCell:(CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 94 break; 95 96 case NSFetchedResultsChangeMove: 97 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 98 [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 99 break; 100 } 101} 102 103- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 104 switch(type) { 105 case NSFetchedResultsChangeInsert: 106 [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 107 break; 108 109 case NSFetchedResultsChangeDelete: 110 [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 111 break; 112 default: 113 break; 114 } 115} 116 117- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller 118{ 119 [self.tableView beginUpdates]; 120} 121 122- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller 123{ 124 [self.tableView endUpdates]; 125} 126 127- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 128 if (editingStyle == UITableViewCellEditingStyleDelete) { 129 130 id managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath]; 131 [managedObject MR_deleteEntity]; 132 [[managedObject managedObjectContext] MR_saveToPersistentStoreAndWait]; 133 } 134} 135 136- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 137 138 [self performSegueWithIdentifier:@"pushDetailView" sender:self]; 139} 140 141- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 142{ 143 return [CustomTableViewCell rowHeight]; 144} 145 146- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 147{ 148 DetailViewController *detailViewController = segue.destinationViewController; 149 150 NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 151 Person *person = (Person *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 152 detailViewController.person = person; 153} 154 155@end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

MagicalRecordのapiをちゃんと確認しましょう。
具体的には下記のメソッドの返値です。

+MR_findAllSortedBy:ascending:withPredicate:inContext:

投稿2014/10/05 03:09

bluedome

総合スコア57

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問