如何填充我的UITableView控制器?

时间:2011-10-09 21:55:23

标签: iphone ios uitableview

jokesArrayNSArray

这是我的Web服务数据:

(
        {
        "_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
        );
    }
)

请指出为什么我的UITableView没有填充。

- (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 == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

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

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

    return cell;
}

2 个答案:

答案 0 :(得分:3)

我认为你需要在分配了jokesArray之后为你的UITableView调用reloadData方法:

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

答案 1 :(得分:1)

尝试在获取数组

后将以下内容添加到代码中
[self.tableView reloadData];

您应该添加此项,因为该表会向代理询问初始化时的行号,并且您的Web服务的响应尚未恢复。

因此,您应该在获得响应后强制执行表重新加载数据,并再次调用委托方法!