【发布时间】:2014-04-08 20:23:55
【问题描述】:
我有一个填充的 tableview 以及一个播放声音的按钮。我希望该按钮根据在 table view 中选择的内容播放不同的声音。
如何在按下按钮时检索表格视图中选择的行?有没有更好的方法来做到这一点?
谢谢,代码如下:
-(void)viewDidLoad
{
[super viewDidLoad];
NSURL *buttonURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"applause_y" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL,
&SoundID);
NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy",
@"Bashful", @"Happy", @"Doc", @"Grumpy", @"Dopey", @"Thorin", @"Dorin", @"Nori", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin", @"Gloin", @"Bifur", @"Bofur", @"Bombur", nil];
self.listData = array;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat: @"You selected %@", rowValue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Row Selected!"
message:message
delegate:nil cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//Button that plays sound
- (IBAction)Button:(id)sender
{
AudioServicesPlaySystemSound(SoundID);
}
【问题讨论】:
-
更新您的
Button方法(顺便说一句 - 方法名称应以小写字母开头)以将声音 id 基于当前选定的行。 -
完成。感谢 rmaddy 的提示。
标签: ios objective-c cocoa-touch uitableview uibutton