从 JSON NSDictionary 创建深入详细信息 UITableView

发布于 2024-09-04 18:36:00 字数 2150 浏览 2 评论 0原文

我在找出如何执行此操作时遇到了很多麻烦,

我想在 UITableDetailView (向下钻取样式)中显示它,每个项目都在不同的单元格中。我读过的所有内容都展示了如何在细节上加载单个图像,而不是显示为 UITableView。字典是否必须有“子项”才能正确加载?以下是第一个视图从 JSON rowsArray 创建 *dict 的代码。

我想我正在寻找的是在 FollowingDetailViewController.m 中放入什么来查看 *dict 的其余内容,因此当选择一行时,它会加载 *dict 的其余部分。

我的 rowsArray 返回如下:

'{ loginName = Johnson;

成员ID=39;

姓名=克里斯;

年龄 = ;} 等等...

谢谢,

// 设置表 &细胞

  - (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];
}
    NSDictionary *dict = [rowsArray objectAtIndex: indexPath.row];

    cell.textLabel.text = [dict objectForKey:@"name"];
    cell.detailTextLabel.text = [dict objectForKey:@"loginName"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    return cell;
}

- (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];



- (void)viewDidLoad {
    [super viewDidLoad];

//GET JSON FROM WEBSERVICES:
    NSURL *url = [NSURL URLWithString:@"http://10.0.1.8/~imac/iphone/jsontest.php"];
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        rowsArray = [dict objectForKey:@"member"];
        [rowsArray retain];
    }


[jsonreturn release];

}

//GO  TO DETAIL VIEW:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FollowingDetailViewController *aFollowDetail = [[FollowingDetailViewController alloc] initWithNibName:@"FollowingDetailView" bundle:nil];
    [self.navigationController pushViewController:aFollowDetail animated:YES];
}

I'm have a load of trouble finding out how to do this,

I want to show this in a UITableDetailView (Drill-Down style) with each item in a different cell. All of the things I've read all show how to load a single image on the detail instead of showing as a UITableView. Does the dictionary have to have "children" in order to load correctly? Here is the code for the first view creating *dict from the JSON rowsArray.

I guess what I'm looking for is what to put in the FollowingDetailViewController.m to see the rest of *dict contents, so when selecting a row, it loads the rest of the *dict.

I have rowsArray coming back as follows:

'{ loginName = Johnson;

memberid = 39;

name = Kris;

age = ;}, etc,etc...

Thanks,

// SET UP TABLE & 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];
}
    NSDictionary *dict = [rowsArray objectAtIndex: indexPath.row];

    cell.textLabel.text = [dict objectForKey:@"name"];
    cell.detailTextLabel.text = [dict objectForKey:@"loginName"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    return cell;
}

- (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];



- (void)viewDidLoad {
    [super viewDidLoad];

//GET JSON FROM WEBSERVICES:
    NSURL *url = [NSURL URLWithString:@"http://10.0.1.8/~imac/iphone/jsontest.php"];
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        rowsArray = [dict objectForKey:@"member"];
        [rowsArray retain];
    }


[jsonreturn release];

}

//GO  TO DETAIL VIEW:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FollowingDetailViewController *aFollowDetail = [[FollowingDetailViewController alloc] initWithNibName:@"FollowingDetailView" bundle:nil];
    [self.navigationController pushViewController:aFollowDetail animated:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-09-11 18:36:00

不要使用阻塞调用来检索 JSON。如果您的网络速度很慢,您的应用程序将挂起。您应该使用异步方法来检索数据。

研究将 JSON 内容加载到模型对象中。然后使用一系列模型来支持您的表格视图。然后,DidSelectRowAtIndexPath 可以通过自定义初始值设定项或通过在分配/初始化它之后设置属性,将模型对象的实例传递到详细视图中。你的问题很难解析,所以我不确定我是否正在解决你所追求的问题。查看开发人员网站上表格视图 UI 层次结构的大量示例。

Don't use a blocking call to retrieve the JSON. If your network is slow your app will hang. You should use an async method to retrieve the data.

Look into loading the JSON contents into a model object. Then use an array of models to power your tableview. DidSelectRowAtIndexPath could then pass a instance of model object into your detail view either through a custom initializer or by setting a property after you alloc/init it. Your question is pretty hard to parse so I'm not sure if I'm addressing what you are after. Take a look at the numerous samples for table view UI hierarchies on the developer site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文