【问题标题】:NSURLConnection delegate and populating table viewNSURLConnection 委托和填充表视图
【发布时间】:2011-01-30 00:34:41
【问题描述】:

我必须调用 NSURLConnection 并拥有以下委托:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData  
        encoding:NSUTF8StringEncoding];
    [responseData release];

    results = [NSArray array];
    results = [responseString JSONValue];
        NSLog(@"Number of rows: %d", [results count]);
}

//table view number of rows    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        //return [_userInfo.friendsInfo count];
        NSLog(@"Number of rows: %d", [results count]);
        return [results count];
    }

如您所见,我尝试打印出数组中的条目数,当在 NSURLConnection 的委托中得到 5,在 tableView numberOfRowsInSection 中得到 0。 我想使用从 NSURLConnection 获得的数据来填充表格。我该怎么做?

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    我看到了两个潜在的问题:

    1. 您缺少保留。行“结果 = [NSArray 数组];”毫无意义,您应该将 JSONValue 保留为“results = [[responseString JSONValue] retain];” (记得在某个时候发布!)
    2. 另外,你是在获取数据后重新加载tableView吗?你应该做类似 [table reloadData];或 [self.tableView reloadData];取决于你的班级结构。

    【讨论】:

      【解决方案2】:

      您是否保留结果指向的数组?我认为您需要调用setResults(如果它是合成的)或显式保留。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多