我在我的 TableView 中实现 SearchBar。但是当我尝试在 SearchBar 中输入文本时它会显示错误。
这是我的 NSObject 类代码 .h 文件:
#import <Foundation/Foundation.h>
@interface ChannelList : NSObject
{
NSString *channelLink;
NSString *channelName;
NSString *channelType;
NSString *channelLogo;
}
@property (nonatomic, copy) NSString *channelName;
@property (nonatomic, copy) NSString *channelType;
@property (nonatomic, copy) NSString *channelLogo;
@property (nonatomic, copy) NSString *channelLink;
+ (id)channelLink:(NSString*)channelLink channelName:(NSString*)achannelName channelType:(NSString*)achannelType channelLogo:(NSString*)achannelLogo;
@end
和 .m 文件:
#import "ChannelList.h"
@implementation ChannelList
@synthesize channelLogo,channelName,channelType,channelLink;
+ (id)channelLink:(NSString*)channelLink channelName:(NSString*)achannelName channelType:(NSString*)achannelType channelLogo:(NSString*)achannelLogo{
ChannelList *ChannelList = [[self alloc] init];
[ChannelList setChannelLink:channelLink];
[ChannelList setChannelName:achannelName];
[ChannelList setChannelType:achannelType];
[ChannelList setChannelLogo:achannelLogo];
return ChannelList;
}
@end
使用这个数组来填充我的表格视图
channelAllData = [NSArray arrayWithObjects:
[ChannelList channelLink:@"http://cdn.m.yuppcdn.net/liveorigin/smil:ndtvhindi_iphone.smil/playlist.m3u8" channelName:@"NDTV" channelType:@"NEWS" channelLogo:@"ndtv.png"],[ChannelList channelLink:@"http://cdn.m.yuppcdn.net/liveorigin/smil:aajtak_iphone.smil/playlist.m3u8" channelName:@"Aaj_Tak" channelType:@"NEWS" channelLogo:@"Aaj_Tak"],[ChannelList channelLink:@"http://cdn.m.yupptv.tv/liveorigin/smil:indiatv.smil/playlist.m3u8" channelName:@"India_tv" channelType:@"NEWS" channelLogo:@"India_tv_logo.gif"],[ChannelList channelLink:@"http://cdn.m.yuppcdn.net/liveorigin/smil:headlinetoday_iphone.smil/playlist.m3u8" channelName:@"HeadlinesToday" channelType:@"NEWS" channelLogo:@"HeadlinesToday.png"],nil];
searchedData = [NSMutableArray arrayWithCapacity:[channelAllData count]];
这里 searchedData 是我的 SearchBar 的搜索结果。
在这里,我填充了我的 searchedData,并得到了我在标题中提到的错误。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
// remove all data that belongs to previous search
[searchedData removeAllObjects];
if([searchText isEqualToString:@""]||searchText==nil){
[self.tableView reloadData];
return;
}
[self filterContentForSearchText:searchText];
// NSLog(@"%i",searchedData.count);
[self.tableView reloadData];
}
- (void)filterContentForSearchText:(NSString*)asearchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",asearchText];
NSLog(@"predicate %@",predicate);
NSArray *tempArray = [channelAllData filteredArrayUsingPredicate:predicate];
searchedData = [NSMutableArray arrayWithArray:tempArray];
}