gpt4 book ai didi

ios - UITableView 未加载但数组已填充

转载 作者:行者123 更新时间:2023-11-28 22:54:16 25 4
gpt4 key购买 nike

我有一个 UITableView,我想通过 WebAPI 填充数据。我已设置好所有内容,并且可以验证数据是否以 JSON 格式返回。我不明白的是为什么我在使用 NSLog 时看到数组中的数据打印了好几次,以及如何将这些数据分配给 UITableView。我能够从 viewDidLoad 方法填充表格,但这些是硬编码值。我想用我对远程服务器的调用返回的数据填充网格。这些数据在我的 didReceiveData 委托(delegate)中可用。我在这里做错了什么?

我的 MasterViewControler.h 中有这段代码

@interface MasterViewController : UITableViewController <UITableViewDataSource, UIAlertViewDelegate> {

NSMutableArray *dataArray;
NSMutableArray *categoryNames;

这是我的 MasterViewController.m 文件中内容的删节版

NSMutableData* receivedData;
NSString* hostName;
NSInteger portNumber = 9999;
NSMutableDictionary* dictionary;
NSInteger maxRetryCount = 5;
int count = 0;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

NSLog(@"Succeeded! Received %d bytes of data",[data length]);
NSError *error = nil;
// Get the JSON data from the website

id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if ([result isKindOfClass:[NSArray class]]) {

for (NSArray *item in result)
[dataArray addObject:item];

NSLog(@"%@", dataArray);
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;

for(NSDictionary *item in jsonDictionary)
NSLog(@"Item: %@", item);
}}

- (void)viewDidLoad{
[super viewDidLoad];

hostName = [[NSString alloc] initWithString:@"12.34.56.78"];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i%@", hostName, portNumber, @"/api/products"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 2];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];

// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)]; //insertNewObject
self.navigationItem.rightBarButtonItem = addButton;

dataArray = [[NSMutableArray alloc] init];
//[dataArray addObject:@"Apple"];
//[dataArray addObject:@"Mango"];
//[dataArray addObject:@"Orange"];}

我没有在此处填充 dataArray,而是试图在 didReceiveData 委托(delegate)中填充它。 dataArray 将被分配,但它几乎就像我必须重新加载 UITableView 才能看到值。我在 didReceiveData 结束时尝试这样做,但收到错误消息。

最佳答案

首先确保您的 UITableViewDataSourceUITableViewDelegate 已设置。

一旦数组被填充(在你的例子中,在 -(void)connection:didReceiveData: 的末尾)你会调用 [tableView reloadData] 来刷新 table 。

然后你将你的单元格设置为:

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

// If your array is an array of strings, change if applicable
NSString *string = [dataArray objectAtIndex:indexPath.row];
[cell.textLabel setText:string];

}

关于ios - UITableView 未加载但数组已填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com