我在 storyboard
中设置了一个 TableView
并有一个用于保存图像的原型(prototype)单元格和一个用于启动视频的按钮。视频文件的路径是每个单元格对象中的一个属性,我想在单击按钮时播放视频。
在我开始使用表格之前(当我刚刚在 Storyboard上手动绘制按钮时)我使用以下命令启动电影播放器 [self presentMoviePlayerViewControllerAnimated:mp];
但显然现在这是不可能的.
我确定我遗漏了一些明显的东西......
最佳答案
最好的解决方案是NSNotificationCenter
。单击时,在单元格中只需发送带有行索引的通知,您可以将其保存在按钮的标记变量中。
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:@"playVideo" forKey:@"operationKey"];
[userInfo setValue: indexPath forKey:@"indexPathRow"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"PlayVideoNotification" object:nil userInfo:userInfo];
在您的 ViewController 中注册观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivePlayVideoNotification:) name:@"PlayVideoNotification" object:nil];
并且不要忘记注销观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PlayVideoNotification" object:nil];
在方法PlayVideoNotification
中执行您需要的操作:
- (void) receivePlayVideoNotification:(NSNotification *) notification
{
NSDictionary *userInfo = notification.userInfo;
NSString *operationKey = [userInfo objectForKey:@"operationKey"];//"playVideo"
NSString* indexPathRow = [userInfo objectForKey:@"indexPathRow"];
//todo
}
关于ios - 从可重用的 UITableViewCell 对象开始视频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23382161/