2014-02-14 235 views
0

我正在研究一個項目,可以讓您在選擇tableview中的單元格時播放聲音。爲了讓事情更容易一些,我已經實現了搜索功能。問題是,這不是很正常。當你點擊一個結果時,播放的聲音來自原始數組,而不是新過濾的聲音。我知道我的錯誤在這裏,我希望找到一些幫助。Flitering搜索結果

//Create new Sound Object 
Sounds *sound = nil; 
//check to see whether the normal table or search results table is being displayed and  set the Sound object from the appropriate array 
if (tableView == self.searchDisplayController.searchResultsTableView) { 

    sound = [filteredSoundArray objectAtIndex:indexPath.row]; 
}else{ 
    sound = [soundArray objectAtIndex:indexPath.row]; 
} 

//Configure the cell 
[[cell textLabel]setText:[sound name]]; 
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
NSString *selectedSoundFile = [[soundArray objectAtIndex:indexPath.row]name]; 
NSString *path = [[NSBundle mainBundle]pathForResource:selectedSoundFile ofType:@"mp3"]; 


if(path){ 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [self.theAudio play]; 
} 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    [self.theAudio play]; 

} 

}

謝謝!

回答

1

變化didSelectRowAtIndexPath

NSString *selectedSoundFile = [[soundArray objectAtIndex:indexPath.row]name]; 

的數組,如果你是從搜索結果中palying然後用filteredSoundArray代替soundArray ..

你玩上選擇的條件應該從陣列中播放您正在使用在表格視圖中顯示。但是你正在使用包含所有聲音的數組。

變化作爲didSelectRowIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

NSString *selectedSoundFile; 
if (tableView == self.searchDisplayController.searchResultsTableView) { 

selectedSoundFile = [[filteredSoundArray objectAtIndex:indexPath.row]name]; 
}else{ 
selectedSoundFile = [[soundArray objectAtIndex:indexPath.row]name]; 
} 
NSString *path = [[NSBundle mainBundle]pathForResource:selectedSoundFile ofType:@"mp3"]; 


if(path){ 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

[self.theAudio play]; 
} 
} 
+0

如下當我改變的NSString * selectedSoundFile = [[soundArray objectAtIndex:indexPath.row]名稱];到NSString * selectedSoundFile = [[filteredSoundArray objectAtIndex:indexPath.row] name];我爲一個空數組收到了一個「NSRangeException」。 – user1176046

+0

過濾的聲音現在工作得很好!但是當我嘗試播放一個我沒有搜索的聲音時,我得到了「NSRangeException」。 – user1176046

+0

請在 – RAJA