【发布时间】:2013-09-09 21:07:17
【问题描述】:
我有一个 UITableView,用户可以在其中单击附件。附件在模态视图中启动 UIView(使用 presentViewController)。
当它返回时,它意味着用所需的数据更新 tableview 单元格。
目前我正在使用单例来捕获字典中的 userData;但即使我使用 NSNotification 我也有同样的问题。
我的问题是,当模式对话框被关闭时,tableview 永远不会更新;然而 如果我滚动或以其他方式物理移动,则表格单元格中的条目会更新。
如果我尝试使用 NSNotifications 强制重新加载单元格或整个表格视图,我会得到相同的结果。
我想知道是否可以得到一些帮助;如何使单元格重新加载?
下面是与单元格显示和模态显示方式相关的代码;
注释;
_eventName is just a NSString
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ( [[MySingleton sharedInstance].userData objectForKey:@"eventName"] !=nil) {
_eventName = [[MySingleton sharedInstance].userData objectForKey:@"eventName"];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSDictionary *dict;
NSString *name;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
dict = [_template objectAtIndex:indexPath.row];
name = [dict objectForKey:@"name"];
cell.textLabel.text = name;
NSString *key = [[dict objectForKey:@"key"] lowercaseString];
if ([[key lowercaseString] isEqualToString:@"name"]==YES) {
cell.detailTextLabel.text = _eventName;
}
return cell;
}
-(void) tableView:(UITableView *)tv accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [_template objectAtIndex:indexPath.row];
NSString *key = [[dict objectForKey:@"key"] lowercaseString];
NSString *name = @"";
if ([key isEqualToString:@"name"]==YES) {
EventNameVC *nameEditor = [[EventNameVC alloc] initWithNibName:@"EventNameVC" bundle:nil];
if (_eventName !=nil) {
nameEditor.txtName.text = _eventName;
} else {
nameEditor.txtName.text = name;
}
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:nameEditor];
// When we return from the view, reload the cell.
[self presentViewController:nav animated:YES completion:^{
[self.tv reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}];
nav = nil;
}
}
编辑:尝试使用通知来检测模式的关闭;但我仍然有同样的问题 - 也就是说,UITableView 仅在我物理移动表格时显示更新的数据。
在我的视图控制器中我这样做;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.notificationObserver = [[NSNotificationCenter defaultCenter]
addObserverForName:@"dismissModalView"
object:nil
queue:mainQueue
usingBlock:^(NSNotification *notification) {
NSLog(@"Notification received!");
NSDictionary *userInfo = notification.userInfo;
_eventName = [userInfo objectForKey:@"eventName"];
NSLog(@"received._eventName = %@", _eventName);
[self performSelectorOnMainThread:@selector(updateTable) withObject:nil waitUntilDone:YES];
}];
}
return self;
}
-(void) updateTable {
NSLog(@"%s", __PRETTY_FUNCTION__);
[self.tv reloadData];
}
我在 accessoryButtonTappedForRowWithIndexPath 方法中启动我的 modalView
EventNameVC *nameEditor = [[EventNameVC alloc] initWithNibName:@"EventNameVC" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:nameEditor];
[self presentViewController:nav animated:YES completion:nil];
好的,所以当我们使用 presentViewController 启动模态视图并在关闭时,会触发通知。
在我的模态视图中
-(IBAction) btnSave:(id)sender {
[self.txtName resignFirstResponder];
///[[MySingleton sharedInstance].userData setObject:self.txtName.text forKey:@"eventName"];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self.txtName.text forKey:@"eventName"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" object:self userInfo:userInfo];
[self dismissViewControllerAnimated:YES completion:nil];
}
当我运行它时,updateTable 会正确触发并登录到控制台。
到目前为止一切顺利。
但是tableview仍然没有刷新它的数据;我仍然需要物理移动表格才能更新单元格。
如何强制单元格正确更新?
【问题讨论】:
-
尝试在您的表格视图上调用
reloadData以强制刷新。 -
当你说把reloadData放到tableView上,用什么方法?您是指在
[self presentViewController:nav animated:YES completion...的完成块中还是其他地方? -
我尝试将 reloadData 放入
viewWillAppear' like[self.tv reloadData];` 这不会导致单元格正确显示。 -
愚蠢的问题:当 reloadData 被调用时,
self.tv是否指向正确的表视图? -
是的。但我会尝试重写整个视图,以防我犯了一个愚蠢的错误
标签: objective-c uitableview reloaddata