Ios tableView未使用数组中的数据填充
我不熟悉iOS开发和Objective-C编程,所以请原谅我的“无趣” 问题是,我有一个函数,它构建一个数组,从JSON对象获取数据。 然后我有第二个函数,负责用数组中包含的数据填充tableView 问题是,这个数组的内容似乎无法从我的tableView函数访问,即使该数组已添加到我的头文件并在主文件中实现。 tableView仍为空 你知道为什么吗 以下是函数,非常感谢您的帮助 1) 阵列构建功能:Ios tableView未使用数组中的数据填充,ios,json,nsmutablearray,tableview,cell,Ios,Json,Nsmutablearray,Tableview,Cell,我不熟悉iOS开发和Objective-C编程,所以请原谅我的“无趣” 问题是,我有一个函数,它构建一个数组,从JSON对象获取数据。 然后我有第二个函数,负责用数组中包含的数据填充tableView 问题是,这个数组的内容似乎无法从我的tableView函数访问,即使该数组已添加到我的头文件并在主文件中实现。 tableView仍为空 你知道为什么吗 以下是函数,非常感谢您的帮助 1) 阵列构建功能: - (void)connection:(NSURLConnection *)connecti
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
//Get data
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
//Extract titles
NSMutableArray *myResults = [[NSMutableArray alloc] init];
NSArray *results = [res objectForKey:@"data"];
for (NSDictionary *result in results) {
NSString *title = [result objectForKey:@"colors"];
NSLog(@"colors: %@", colors);
[myResults addObject:colors];
}
self.colorNames = myResults;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
//Set cell text
NSString *colorString = [self.colorNames objectAtIndex: [indexPath row]];
NSLog(@"%@",colorString);
cell.textLabel.text = colorString;
return cell;
}
2) TableView填充功能:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
//Get data
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
//Extract titles
NSMutableArray *myResults = [[NSMutableArray alloc] init];
NSArray *results = [res objectForKey:@"data"];
for (NSDictionary *result in results) {
NSString *title = [result objectForKey:@"colors"];
NSLog(@"colors: %@", colors);
[myResults addObject:colors];
}
self.colorNames = myResults;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
//Set cell text
NSString *colorString = [self.colorNames objectAtIndex: [indexPath row]];
NSLog(@"%@",colorString);
cell.textLabel.text = colorString;
return cell;
}
您确定在收到json数据后重新加载表视图数据吗?我建议使用
[self.tableView reloadData]
在连接的末尾:didReceiveData:
方法。否则,加载视图后,只会调用一次方法tableView:cellforrowatinexpath:
干杯,
anka我想您必须实现更多的UITableViewDataSource协议。 在调用tableView:cellForRowAtIndexPath:用数据填充表格之前,必须至少提供一个返回表格实际行数的方法tableView:numberOfRowsInSection: 祝福
Bruno行是一种索引方法吗?我认为应该是indexPath.row而不是[indexPath row]控制器是TableViewController还是包含TableView的ViewController?谢谢您的回答!我真的没有重新加载。我可能很愚蠢,但是[self.tableView重载数据];抛出一个错误“在myViewController类型的对象上找不到tableView”。我怎样才能解决这个问题?再次感谢!好的,我以为您使用的是UITableViewController的子类?如果使用简单的UIViewController,则必须在主视图中保留表视图的引用/出口。然后在此表视图的实例上调用
reloadData
方法。我没有表视图的出口,添加了一个,并按照您的建议放置了重新加载行,效果非常好,您正式成为我的英雄。万分感谢!感谢Bruno,上面建议的解决方案实际上工作得很好,我已经有了一个获取实际行数的方法。在Objective C中,您可以通过两种方式调用方法或访问属性。