Elton's Blog

UITableView reloadData或viewWillAppear失效

by on 十一.24, 2009, under iPhone

今天在做开发的时候,忽然发现在视图的viewWillAppear:方法中添加:

1
[self.tableView reloadData];

不起作用,viewWillAppear:这个方法根本没有调用

后来发现原来用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以达到这个目的。

所以要做到这个,你必须做以下几步:

1. 设置代理类

1
nav.delegate = self;

2. 代理类实现UINavigationControllerDelegate Protocol
3. 在代理类中添加– navigationController:willShowViewController:animated:方法

如:

1
2
3
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
	[self.myTableView reloadData];
}




:, , , ,

1 Comment for this entry

  • 于宙

    看了你的文章很受启发,我这还有个相关的问题,我在– tableView:viewForHeaderInSection:里面返回了一个UISearchBar,然后在代理方法- (void)searchBar:textDidChange:里面[self.tableView reloadData];结果就是每次一输入,uisearchbar的文本框被置空,也被取消了第一响应状态,这个跟reloaddata有关系吗?

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...