Ios 如何使用NSNotificationCenter发送数据,并在收到数据后重新加载UITable?
我在Ios 如何使用NSNotificationCenter发送数据,并在收到数据后重新加载UITable?,ios,objective-c,uitableview,nsnotificationcenter,Ios,Objective C,Uitableview,Nsnotificationcenter,我在viewController1中有一个NSNotification,它发送对象:selectedTableString 当在viewController1中调用didselectrowatinexpath:方法时 对象:selectedTableString对象然后在viewController2中拾取。控制台输出正确,并且正在正确拾取对象。我面临的问题是如何在viewController2中收到通知时重新加载([mainTable reloadData];)tableView -(void
viewController1
中有一个NSNotification
,它发送对象:selectedTableString
当在viewController1
中调用didselectrowatinexpath:
方法时
对象:selectedTableString
对象然后在viewController2
中拾取。控制台输出正确,并且正在正确拾取对象。我面临的问题是如何在viewController2
中收到通知时重新加载([mainTable reloadData];
)tableView
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedTableString = cell.textLabel.text;
if (cell.accessoryType==UITableViewCellAccessoryNone)
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
if (prev!=indexPath.row) {
cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prev inSection:0]];
cell.accessoryType=UITableViewCellAccessoryNone;
prev=indexPath.row;
}
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
NSLog(@"selectedTableString is %@",selectedTableString);
NSLog(@"posting notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:selectedTableString];
}
然后在视图控制器2
中:
- (void)selectedList:(NSNotification*)notification
{
NSLog(@"received notification");
theString = [notification object];
NSLog(@"thestring is %@", theString);
// getting an NSString
selectedTableString = theString;
if ([selectedTableString isEqualToString:@"Italy"]) {
jsonStringCountry = @"http://***/v1/public/cities?country=it";
[mainTable reloadData];
}
else if ([selectedTableString isEqualToString:@"Spain"]) {
jsonStringCountry = @"http://***/v1/public/cities?country=es";
[mainTable reloadData];
}
else if ([selectedTableString isEqualToString:@"UK"]) {
jsonStringCountry = @"http://***/v1/public/cities?country=uk";
}
else if ([selectedTableString isEqualToString:@"Brazil"]) {
jsonStringCountry = @"http://***/v1/public/cities?country=br";
}
NSLog(@"from two table selectedTableString %@",selectedTableString);
[mainTable reloadData];
}
这是你关于同一问题的第三个问题。请重新表述您之前的问题,没有必要反复问同样的问题。Dude使用second view controller中的属性并直接设置。或者使用委托。如果有一种简单的方法可以解释为什么要这样做?调用reloadData时mainTable是否非空?如果两个表都可见,则使用通知的效率似乎很低。那么问题是什么?您说问题是如何重新加载表——您在这段代码中就是这么做的。什么不起作用?