【发布时间】:2014-09-23 18:42:56
【问题描述】:
这是我的自定义 NSObject 类:
@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];
在 tableViewcellForRowAtIndexPath 方法中并使用它来代替。但我得到一个不同的错误——原因:
'[<personObject 0x109524750> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Paul Smith.'
当我开始在搜索栏中输入内容时,这两个错误都会发生。
这里是row方法...如何在搜索数组中将单元格文本设置为currentPerson.personName
- (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;
}
【问题讨论】:
标签: ios xcode nspredicate predicate