Elton's Blog

Tag: UITableView

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 :, , , , more...

loadView时候的注意事项

by on 十一.17, 2009, under iPhone

loadView是使用代码生成视图的时候,当视图第一次载入的时候调用的方法。

今天写一个Navigation Controller,给它加一个继承了TableViewController的View作为Navigation Controller的Root View

1
2
3
4
5
UITableViewController *tableViewController = [[TableViewController alloc] init];
 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: tableViewController];
nav.navigationBar.barStyle = UIBarStyleBlack;
[self.view addSubview:nav.view];

一切都很顺利成章,但是运行的时候,除了显示一个NavigationBar以外,表格并没有显示出来。 反复查找原因都为果。 后来仔细对比之前的代码,原来是tablebView中的loadView方法少写了下面这行代码:

1
[super loadView];

后来查了文档,说的很清楚,自己的类里面的loadView一定要第一行加上[super loadView]这句话。 看来以后还是要多注意细节。

1 Comment :, , , more...

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...