gpt4 book ai didi

ios - xcode - 从另一个类调用方法后刷新 TableView

转载 作者:行者123 更新时间:2023-11-29 02:41:31 25 4
gpt4 key购买 nike

我创建按钮来添加学生,我让方法通过显示弹出菜单(警报 View )来处理它然后获取在名为 result (NSString) 的变量中输入的代码,然后调用 web 服务从我的 web 服务获取所需的数据

- (IBAction)add_button:(id)sender {
[self addStudent];
}

- (void) addStudent {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Student" message:@"Enter Connection Code" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Add", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
result = [[alertView textFieldAtIndex:0] text];
NSLog(@"the result connection code %@",result);
WebService *web = [[WebService alloc]init];
[web retriveDataWithCodeAndUrl:result Andurl:STUDENT_URL];
}

在网络服务类中完成加载并获取所需数据后我需要恢复我的 tableview 并用新数据刷新它我尝试了协议(protocol),但它没有用,所以我尝试创建我的 tableview 类的实例,我也尝试发出通知来处理它并刷新 View (网络服务类)

-(void) connectionDidFinishLoading :(NSURLConnection *) connection{
NSLog(@"data finish loading ");
School_TableViewController *home = [[School_TableViewController alloc]init];
[home didFinishWithData:respone];
[[self delegate]didFinishLoadingwithData:respone];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
}

回到我的 TableView 类来处理通知和刷新表

- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
[self.tableView reloadData];
}

但它不起作用,我需要在单例和 TableView 之间进行通信的好方法以及刷新 View 的方法

提前谢谢

最佳答案

当然它不起作用,因为你没有为你的tableView更新数据

我想知道你为什么不使用 delegate didFinishLoadingwithData

使用委托(delegate):

-在 School_TableViewController.h 中:

@interface School_TableViewController : UITableViewController <YourWebServiceDelegate>

-在 School_TableViewController.m 中:

 -(void) didFinishLoadingwithData:(NSData*) data {
// Do parse respone data method and update yourTableViewData
yourtableViewData = notification.object
[self.tableView reloadData];
}

或者试试

-在服务级别

-(void) connectionDidFinishLoading :(NSURLConnection *) connection{
NSLog(@"data finish loading ");
/* remove this code you don't need alloc School_TableViewController
School_TableViewController *home = [[School_TableViewController alloc]init];
[home didFinishWithData:respone];
*/
[[self delegate]didFinishLoadingwithData:respone];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:response];
}

-在你的tableView中

- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
// Do parse respone data method and update yourTableViewData
yourtableViewData = notification.object
[self.tableView reloadData];

}

关于ios - xcode - 从另一个类调用方法后刷新 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25719576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com