2012-07-02 70 views
1

我正在使用UITableView控件来显示一些可以由用户编辑的数据。为了编辑细节,用户点击编辑按钮,将新视图推入堆栈。用户编辑数据,点击保存按钮,数据被保存到plist中,视图弹出堆栈。即使plist已更新,UITableView仍会显示旧数据。这可以通过在viewWillAppear方法中添加对reloadData的调用来解决。但是,当视图第一次加载数据显示正确,通过添加重载语句这是否意味着双重绑定?如果是这样,这怎么能避免?UITableView刷新查询

我发现下面的代码(here),它强制进行刷新,而不显式调用reloadData:

- (void) viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
int orientation = [[UIDevice currentDevice] orientation]; 
if(orientation != UIDeviceOrientationUnknown) 
    [self willRotateToInterfaceOrientation:orientation duration:0]; 
} 

谁能解释如何/为什么这样的作品?

回答

1

您链接的窍门是肮脏的黑客攻击。它不仅重新加载数据,还强制重绘表格。它会告诉你的应用程序,该设备正在获得新的方向,所以你的表格会与其他UI元素一起重新绘制。

在您的UITableView中刷新一行或一组特定行的标准方法是调用它的reloadRowsAtIndexPaths:withRowAnimation:方法:这样做会调用您的数据源以仅获取已更新的行的数据,防止完全重新加载。

1

这样做:

- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    //remove all objects from yourTableViewDataSourceArray 
    [yourTableViewDataSourceArray removeAllObjects]; 

    //add new records from plist 
    yourTableViewDataSourceArray = plist request of data here 

    //reload table now 
    [yourtableView reloadData]; 
}