2014-09-23 119 views
1

。這是我的自定義NSObject類:NSPredicate過濾通過的數組對象

@interface personObject : NSObject 

@property (nonatomic, copy) NSString *personName; 
@property (nonatomic, copy) UIImage *personPhoto; 

@property BOOL invited; 

- (id) initWithName: (NSString *) personName Photo: (UIImage *) photo Invited: (BOOL *) invited; 

@end 

@implementation personObject 
@synthesize personName = _personName; 
@synthesize personPhoto = _personPhoto; 
@synthesize invited = _invited; 

- (id) initWithName:(NSString *)personName Photo:(UIImage *)photo Invited:(BOOL *)invited{ 
    self = [super init]; 
    if (self) { 
     self.personName = personName; 
     self.personPhoto = photo; 
     self.invited = invited; 
    } 
    return self; 
} 

@end 

我初始化三人在viewDidLoad中的對象。 現在我試圖

[NSPredicate predicateWithFormat:@"personName beginswith[c], searchText]; 

但我得到一個錯誤 -

-[personObject copyWithZone:]: unrecognized selector sent to instance 0x109684330' 

所以,我想這:

- (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{ 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginsWith[c] %@",_currentPerson.personName, searchText ]; 
    self.searchArray = [self.peopleArray filteredArrayUsingPredicate:predicate]; 
} 

所以我試圖設置

_currentPerson = [self.peopleArray objectAtIndex.row]; 

tableViewcellForRowAt IndexPath方法,並用它來代替。但我得到一個不同的錯誤 - 原因:

'[<personObject 0x109524750> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Paul Smith.' 

這兩個錯誤發生在我開始鍵入搜索欄時。

繼承人的排法......如何設置單元格文本搜索陣列

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    personObject *currentPerson = [self.peopleArray objectAtIndex:indexPath.row]; 

    self.personCell = (personCell *) [self.tableView dequeueReusableCellWithIdentifier:@"personCell"]; 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     self.personCell.nameLabel.text = [self.searchArray objectAtIndex:indexPath.row]; 
    } 
    else{ 
     self.personCell.nameLabel.text = currentPerson.personName; 
    } 
    return _personCell; 
} 

回答

0

您需要檢查personName關鍵不_currentPerson.personName作爲key.When你給_currentPerson.personName中currentPerson.personName它採取它的關鍵,並認爲它是應該是NSCoding compliant.But你不需要它整個鍵你可以通過只@"personName"爲字符串,它會通過keyValueCoding .Replace工作上面的函數下面

- (void) filterContententForSearchText: (NSString *) searchText scope:(NSString *) scope{ 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginsWith[c] %@",@"personName", searchText ]; 
    self.searchArray = [self.peopleArray filteredArrayUsingPredicate:predicate]; 
    NSLog(@"%@",seachArray); 
} 
+0

仍然得到一個錯誤----原因:' - [personObject copyWithZone:]:無法識別的選擇器發送到實例0x1097316a0' – Peter 2014-09-23 19:21:38

+0

。它正在執行罰款我check.You也不需要BOOL *只是使用BOOL.'-( id)initWithName:(NSString *)personName照片:(UIImage *)photo Invited:(BOOL)invite;' – codester 2014-09-23 19:26:04

+0

可能是你正在做更多的事情。在哪一行崩潰 – codester 2014-09-23 19:27:14