【问题标题】:xcode - refresh tableview after called method from anther classxcode - 从另一个类调用方法后刷新表视图
【发布时间】:2014-09-08 07:49:11
【问题描述】:

我创建按钮来添加学生,我通过显示弹出菜单(警报视图)来处理它 然后将代码输入到名为 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 并用新数据刷新它 我尝试了协议,但它不起作用,所以我尝试制作我的 tableview 类的实例 ,我也尝试发出通知来处理它并刷新视图 (网络服务类)

-(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];
     }

回到我的表格视图类来处理通知和刷新表格

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

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

提前谢谢你

【问题讨论】:

  • 因为您没有将任何新数据传递给您的表格视图。您只需使用旧数据调用 reload 方法。因此,当您获得 Web 服务的响应时,请解析它或根据您的逻辑将其简单地传递给 tableview,然后调用 reload。
  • 我没有传递新数据,我只是用新数据填充我的数组,但是当它返回通知方法时它返回 nil(names array),我在通知方法中传递新数据
  • 你在通知中发送“self”并且没有数据,而且我没有看到 newdata 在 receiveTestNotification 方法中填充数组的位置

标签: ios iphone xcode uitableview


【解决方案1】:

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

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

使用委托:

-在 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];

}

【讨论】:

  • 我的代表不工作,我想知道为什么?另一种方式工作,但请解释更多如何使用委托来传递数据
  • 我认为您在 School_TableViewController 中没有 webservice.delegate = self ,因此您的委托无法正常工作。很抱歉,因为我不擅长英语并解释它你应该尝试阅读developer.apple.com/library/mac/documentation/general/…
猜你喜欢
  • 1970-01-01
  • 2018-12-11
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多