iOS搜索结果重复

时间:2015-05-08 09:32:33

标签: ios uitableview search uitextfield nsnotifications

我有一个UItableView,里面装满了从服务器上获取的歌曲。滚动桌面视图时,我正在加载10×10的歌曲。我想为此添加搜索功能。所以我使用了UITextField。这就是我实现搜索的方式。

viewDidLoad

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(textFieldTextDidChangeOneCI:)
 name:UITextFieldTextDidChangeNotification
 object:srchtextbox];



-(void)textFieldTextDidChangeOneCI:(NSNotification *)notification
{


[NSObject cancelPreviousPerformRequestsWithTarget:self
                                         selector:@selector(GetArtistSongs)
                                           object:nil];

[NSObject cancelPreviousPerformRequestsWithTarget:self
                                         selector:@selector(SearchLocalSong)
                                           object:nil];



currentSrchStr = srchtextbox.text;
songstable.hidden=YES;
loading=YES;
songArray=[[NSMutableArray alloc]init];
datacameNull=NO;
startRec=0;

if (dm.isfromLocal) {
    [self performSelectorInBackground:@selector(SearchLocalSong) withObject:nil];
}
else
{
     [self performSelectorInBackground:@selector(GetArtistSongs) withObject:nil];
}



}

我的GetArtistSongs方法

-(void)GetArtistSongs
{
NSMutableArray *newsongarray;
if(dm.isfromLocal)
{
    newsongarray=[dbaccess GetLocalPlaylistSongs:lblArtistname.text];
    [songArray removeAllObjects];
}

else
{
    newsongarray=[ws GetPlaylistSongsByPage:selectedPlaylistID :currentSrchStr :0 :0 :0 :0 :startRec :recCount];
}



if (newsongarray!=nil) {
    [songArray addObjectsFromArray:newsongarray];

}
else
{
    songcameNull=YES;
}

[self performSelectorOnMainThread:@selector(songsLoaded) withObject:nil waitUntilDone:NO];
}

然后

-(void)songsLoaded{

loading=NO;
[mainactivityindicator stopAnimating];

[songstable reloadData];
songstable.hidden=NO;
songstable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

我的问题是得到重复的结果。正在搜索。但如果有一首名为“歌曲ABC”的歌曲,它会重复多次。当我检查日志猫中的SongArray时,它会多次包含相同的歌曲。应该是什么原因。请帮我。 感谢

0 个答案:

没有答案