2012-08-23 40 views
1

这是我的场景, 我有ViewController1和Class1(服务类)。UITableView变成了无从服务器得到响应后

我在nib中通过在ViewController1中设置委托和数据源来加载tableView。在viewDidLoad中,我在另一个类(Class1)中调用了一个networkCall函数。在Class1中,获得响应后,它将传递一组响应数据给ViewController1中的一个函数,在这里数据应该填充到tableview中。

我已经在xib中连接了数据源和委托。 问题: 当我在ViewController1中得到一个数组响应时,UITableView变为零,我无法使用reloadData,但是我的数组包含来自服务器的项目列表。

这里是我的代码

ViewController1

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ClassA *class = [[ClassA alloc]init]; 
    [class getResponse]; 

} 

//This method is calling from ClassA using delegate 
-(void)responseData:(NSArray*)arrayList 
{ 
//arrayList have response data 
[tableView reloadData];//here tableView becomes nil. 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"array count %d",array.count);//has number of items(for me, its 3). 
    return array.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 



    static NSString *CellIdentifier = @"TableView"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    cell.textLabel.text = @"dsds"; 

    return cell; 
} 

的tableView呼吁第一次。在接口

在ViewController1 ,我设置的协议

<UITableViewDelegate,UITableViewDataSource> 
+0

'(无效)responseData'方法尝试'yourTableView = [[UITableView的页头] INIT]; [yourTableView reloadData];' – RomanHouse

+0

你似乎没有在你的' - (void)viewDidLoad'中设置你的视图控制器作为'ClassA * class'的委托。你到底该如何从'ClassA'调用' - (void)responseData:(NSArray *)arrayList'? – Vladimir

+0

@Vladimir: id view1 = [[ViewController1 alloc] init]; [view1 responseData:objects]; 这就是我打电话 – Anish

回答

2

您正在创建的ViewController1新实例,而不是使用已经加载的一个。

,你可以做到以下几点:

为ClassA的:

接口:

@interface ClassA : ... 
    @property (weak) ViewController1 * vcDelegate; 
... 
@end 

实现:

@implementation ClassA 
    @synthesize vcDelegate; 
... 
@end 

,而不是和

id<ViewController1Protocol>view1 = [[ViewController1 alloc]init]; 
    [view1 responseData:objects]; 

呼叫

[vcDelegate responseData:objects]; 

在您的视图控制器,创建ClassA当你需要委托设置为self:

- (void)viewDidLoad 
{ 
     [super viewDidLoad]; 
     ClassA *class = [[ClassA alloc]init]; 

     [class setVcDelegate: self]; 

     [class getResponse];   
} 

这不是最好的实现,但应该给你如何做到这一点的想法。

例如,属性可能应该

@property (weak) id<ViewController1Protocol> vcDelegate; 
+0

它像一个魅力工作。我忘了想到新的alloc会创建新的实例// id view1 = [[ViewController1 alloc] init];感谢您的回答 – Anish

+0

很高兴帮助=) – Vladimir

0

你有你的tableView与厦门国际银行的表视图连接。

图像中的红色区域未与您的表格视图相关联。它是空的。

enter image description here

+0

我已经与xib连接。问题是当我加载一个viewcontroller1,tableview委托方法正在调用。但是当我得到responseData函数中的服务器响应时,tableView变为零,我无法使用reloadData。 – Anish

+0

@Anish你已经正确连接了数据源和委托,这就是为什么这些方法被调用。你必须连接IBOutlet和xib –

+0

中的引用插座,但是在从服务器获得响应之后,tableview将不会变为。这是我所看到的。所以当我使用reloadData时,tableView没有被调用。 – Anish

相关问题