2012-12-21 108 views
1

我.h文件中:“块” 的方法,因为numberOfRowsInSection返回0

#import <UIKit/UIKit.h> 
#import "ServiceConnector.h" 

@interface PartnerTableController : UITableViewController <ServiceConnectorDelegate> 

@property (nonatomic, retain) IBOutlet NSMutableArray *allPartners; 


- (IBAction)download:(id)sender; 

@end 

我.m文件的部分:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self download:self]; 
} 

... 

- (IBAction)download:(id)sender { //perform get request 
    ServiceConnector *serviceConnector = [[ServiceConnector alloc] init]; 
    serviceConnector.delegate = self; 
    [serviceConnector download]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    NSLog(@"count allpartners: %d", [allPartners count]); 
    return [allPartners count]; 
} 

的问题是,numberOfRowsInSection返回0,因为它需要一段时间下载我的JSON字符串,创建一个对象,并把它放入NSMutableArray allPartners。我可以以某种方式阻止方法download

更新:allPartners用这种方法填充:

[serviceConnector download]; 

-(void)download{ 

    //build up the request that is to be sent to the server 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.bala.com"]]; 

    [request setHTTPMethod:@"GET"]; 
    [request addValue:@"getValues" forHTTPHeaderField:@"METHOD"]; //selects what task the server will perform 

    //initialize an NSURLConnection with the request 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if(!connection){ 
     NSLog(@"Connection Failed"); 
    } 
} 
+0

当ServiceConnector通过委托通知您它有数据时[table reloadData];就这样。同时你将有一个空表,并可能会显示一些等待的HUD,或者执行一些逻辑,如“如果[所有伙伴计数]为0,则返回计数1并用一些特殊内容填充单元格”。 – demosten

+0

请为下载方法添加代码。 –

+0

你能告诉我如何调用[table reloadData];从另一个班级? – doonot

回答

0

当ServiceConnector通知您通过它你做[table reloadData];数据的代表,这一切。同时你将有一张空桌子,可能会在那里显示一些等待的HUD,或者执行一些逻辑如if [allPartners count] is 0 then return count 1 and fill the cell with some special content

相关问题