我已经阅读了很多关于我的问题,我想我需要使用委托,但我真的尝试过很多东西而且我无法让它发挥作用。我是初学者,所以我可能会遗漏一些必要的东西!
所以这就是我所拥有的:
我有一个分裂视图;在一侧,我有一个collectionView和另一侧的tableView。我想要实现的是将数据从collectionView传递给tableView。当我点击集合中的一个项目时,我希望它出现在表格中。所以我的表格显示了一个数组,它运行正常,每次我点击集合中的一个项目时,它都会在数组中添加一个字典。这很好用。 当我在collectionView的didSelectItemAtIndexPath中尝试重新加载tableView时出现问题。
当我在tableView中创建一个带有[self.tableView reloadData]动作的按钮时,它可以正常工作。我只是无法从collectionView中做到这一点。
我试过了: - Can't use reloadData from another class - iOS how to acces [tableView reloadData] from another class
还有更多,但无法使其发挥作用。
谢谢你的帮助,
版
答案 0 :(得分:1)
您可以使用NSNotificationCenter在2个不同的视图之间进行调用。在TableViewController中,将以下代码添加到其viewDidLoad方法:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveReloadTableViewNotification:)
name:@"reloadTableViewNotification"
object:nil];
然后在TableViewContoller中进一步添加此方法:
- (void)receiveReloadTableViewNotification:(NSNotification *) notification
{
[self.tableView reloadData];
}
现在,在您的CollectionView中将此添加到按钮的IBAction:
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableViewNotification"
object:self];