【问题标题】:How to make tableView reload if Session complete download Data for it?如果 Session 完成下载数据,如何使 tableView 重新加载?
【发布时间】:2014-12-17 09:40:31
【问题描述】:

我的应用程序从 URL 获取数据。我使用 NSURLSession 异步下载数据。 数据将在 Tableview 中重新排列和显示。所以一切都需要在 viewDidload 上准备好。

问题是,异步意味着数据准备会比主线程的速度慢 1 秒。

View loadded , Tableview 得到 0 条数据 , 显示 0 条数据。

1 秒后,第二个线程运行完成处理程序,并将来自互联网的数据存储到 @property 中。

但是 tableView 加载了 NOTHING DISPLAY ,需要重新加载。

  • 第一种解决方案

是的,我可以解决这个问题,我只需调用 [self.delegate.tableView reloadData];在 GDC 主线程内部(在完成处理程序内部),但它似乎违反了 MVC 模式!。

  • 第二个解决方案

所以我需要找出第二个解决方案,让 ViewController 为其自己的模型实现委托,因此 GDC 中的代码将是 [self reloadTableViewData]; 而不是 [self. delegate.tableView reloadData]

而这些 -(void) reloadTableViewData 将由 Viewcontroller 实现

  • 第二个解决方案失败

不幸的是,我的第二个解决方案失败了 “无法识别的选择器发送到实例模型”

  • 我的问题

1)使用异步数据URL获取重新加载TableView的正确解决方案,如何通知并让视图控制器知道“您的数据已准备好,重新加载它”

2) 为什么我的第二个解决方案失败了? (我的委托有什么问题??这似乎是我委托的错误原因)

这是我第二个解决方案的基本代码测试,同样的错误“无法识别的选择器发送到实例......”

Viewcontroller.h

 #import <UIKit/UIKit.h>

 @class Protocol_Holder;

 @interface ViewController : UIViewController

 @property (strong ,nonatomic) Protocol_Holder* holderObject;

 @end

Viewcontroller.m

 #import "ViewController.h"
 #import "Protocol_Holder.h"

 @interface ViewController () <myDelegate>
 @end

 @implementation ViewController

 -(Protocol_Holder*) holderObject{
     if(!_holderObject){
         _holderObject =  [[Protocol_Holder alloc]init];
     }
     return _holderObject;
 }

 - (IBAction)ReloadButton:(UIButton *)sender {

     NSLog(@"Button Pushed");
     [self.holderObject reload_container];
 }

 - (void)viewDidLoad
 {
     [super viewDidLoad];
      self.holderObject.delegate = self;

 }


 -(void) reload_Model{
     // this is where [self.tableView reloadData] should begin;
     NSLog(@"Enemy Reload Reload Reload Reload!!");
 }

 @end

Protocol_Holder.h

 #import <Foundation/Foundation.h>

 protocol myDelegate <NSObject>

 -(void) reload_Model;

 @end


 @interface Protocol_Holder : NSObject {id<myDelegate> delegate;}

 @property (weak,nonatomic) id<myDelegate> delegate;

 -(void) reload_container;

 @end

Protocol_Holder.m

 #import "Protocol_Holder.h"

 @interface Protocol_Holder() <myDelegate>

 @end

 @implementation Protocol_Holder

 -(void) reload_container{
     [self reload_Model];
 }
 @end

【问题讨论】:

    标签: objective-c uitableview delegates


    【解决方案1】:

    你是wrong,一切都需要在viewDidload 上使用ready,因为request 代表downloading data 将在那时你@98765own time depending 上使用它的own time depending connectivity @displaying message那个Please wait as downloding neccessary data

    Downloading request可以添加到viewDidload方法中

    你的tableView 也应该有header displaying message no records

    download completes 时你可以reload tableView 或者如果download fails 然后alert user displaying appropriate message

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多