【问题标题】:Displaying data in UITableView在 UITableView 中显示数据
【发布时间】:2012-08-10 23:43:36
【问题描述】:

我有一个用于 iPad 的拆分视图控制器,左侧有一个向下钻取表。我能够填充我的第一个表格视图,当我单击一个单元格时,这会将我带到我的第二个表格视图。我可以使用 NSLog 命令在命令寡妇的表视图输出中看到返回的记录计数和我希望看到的实际数据。我没有看到表格视图中的实际数据。相反,我看到 UITableViewCellAccessoryDisclosureIndicator 为返回的每一行但没有实际数据。

我正在使用 xib 文件,并且我已经为我希望在钻取中显示的产品创建了此文件。在我的 Product.xib 文件中,我将 File's Owner Outlets 作为 productsTableView 链接到产品(我的 UITableView 控件)和链接到 View 的视图。 View 的引用 Outlets 将 dataSource 链接到产品,delegate 链接到产品,最后将 view 链接到 File's Owner。

我在这里遗漏了什么吗?就像我说的,我得到了所有的数据,只是没有绑定到网格。

@interface ProductViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {

NSMutableArray *listOfItems;
NSMutableArray *dataArray;
IBOutlet UITableView *productsTableView;
}

@property(nonatomic, strong) IBOutlet UITableView *productsTableView;

@end


- (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];
}

// Set up the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = cellValue;

return cell;
}

为了全面起见,我还将发布此内容,以说明我从哪里获取数据以及我是如何做到这一点的......

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

NSError *error = nil;
// Get the JSON data from the website
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];

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

    for (NSArray *item in result) {
        NSArray *products = [item valueForKey:@"ProductDescription"];
        [dataArray addObject:products];
        [listOfItems addObject:products];
    }
}
else {
    NSDictionary *jsonDictionary = (NSDictionary *)result;

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

[self performSelector:(@selector(refreshDisplay:)) withObject:(self.productsTableView) afterDelay:1.0];
NSLog(@"Finished");
}

我的 NSLog 在这里:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"Dictionary: %@", dataArray);
return [dataArray count];
}

【问题讨论】:

  • 您需要发布更多代码,特别是第二个表的 tableView:cellForRowAtIndexPath: 方法。
  • 这是我的 cellForRowAtIndexPath 但我不认为这是问题所在。就像我说的,我看到所有返回的数据都没有问题。我想我错过了将 Products.xib 与另一个函数中返回的 dataArray 连接起来的东西。
  • 但是你说你在日志语句中看到了预期的数据,而不是在你的表中,所以这就是为什么我认为问题可能出在这个方法上。你是从这个方法中记录下来的吗?如果没有,请尝试记录 cellValue 以确保数据到达程序中的那个点。
  • 我添加了 NSLog(@"Cell value: %@", cellValue);在 cellForRowAtIndexPath 的底部,我为每一行返回一个(null)。那么我是否正确假设虽然我正在取回数据但我没有将该数据连接到 cellValue 变量?如果是这样,我该如何解决这个问题?
  • rdelmar,看来我没有引用正确的变量。我应该一直在看 dataArray 而不是 listOfItems。

标签: ios uitableview xib


【解决方案1】:

简单的混合...

  NSString *cellValue = [dataArray objectAtIndex:indexPath.row];

而不是

  NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];

【讨论】:

    【解决方案2】:

    您正在将products(即NSArray)添加到listOfItems。稍后在cellForRowAtIndexPath 中,您说cellValue (NSString) = [listOfItems objectAtIndex:indexPath.row](可能是一个`NSArray')。这将如何运作?

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多