开发者

How can I make my UITableView Controller populate?

开发者 https://www.devze.com 2023-04-11 15:42 出处:网络
jokesArray is a NSArray This is my Data from Web service: ( { \"_id\" ={ \"$id\" = 4e91fd49c7e24cda74000000;

jokesArray is a NSArray

This is my Data from Web service:

(
        {
        "_id" =         {
            "$id" = 4e91fd49c7e24cda74000000;
        };
        author = draper;
        comments =         (
                        {
                author = adias;
                comment = "amazing again";
            }
        );
        created =         {
            sec = 1318118400;
            usec = 0;
        };
        text = "This is a random post again";
        title = "post # 2";
        type =         (
            punjabi
        );
    },
        {
        "_id" =         {
            "$id" = 4e8cf1d6c7e24c063e000000;
        };
        author = faisal;
        comments =         (
                        {
                author = adias;
                comment = amazing;
            },
                        {
                author = nike;
                comment = "I concur";
            }
        );
        created =         {
            sec = 1317772800;
            usec = 0;
        };
        text = "This is a random post";
        title = "post # 1";
        type =         (
            punjabi
        );
    }
)

Please point me as to why my UITableView is not getting populated.

- (void)viewDidLoad
{
    [super viewDidLoad];
    jokesArray = [[NSArray alloc]init];
    [self getJokes];
}


-(void) getJokes
{
    NSURL *url = [NSURL URLWithString:@"http://someurl"];
    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setCompletionBlock:^{
        // Use when fetching text data
        NSString *responseString = [request responseString];

        NSDictionary *resultsDictionary = [responseString objectFromJSONString];


        jokesArray = [resultsDictionary allValues];

        // Use when fetching binary data
  //      NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
    }];
    [request startAsynchronous];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [jokesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == ni开发者_JS百科l) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

  //  [[cell textLabel]setText:[jokesArray objectAtIndex:[indexPath row]]];    

    [[cell textLabel]setText:@"ok"];   

    return cell;
}


I think you need call reloadData method for your UITableView after assigned jokesArray:

jokesArray = [resultsDictionary allValues];
[jokesArray retain]; // May be need if this is not retained property
[self.tableView reloadData];


try adding the following to you code after you get the array

[self.tableView reloadData];

You should add this because the table ask the delegate the row number on initialization and the response from your web service did not got back yet.

therefor, you should enforce the table to reload the data after you got your response and the delegate method will be called again!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号