【问题标题】:Putting data from SONObjectWithData to UITableView将数据从 SONObjectWithData 放到 UITableView
【发布时间】:2014-05-29 09:08:09
【问题描述】:

我正在构建一个应用程序,在该应用程序中,我从 php 文件中获取数据,并且已经在 xcode 中对其进行了 NSLoging,它正在以这种格式显示数据:

jsonObject=(
        (
        1,
        abc,
        "abc@xyz.com",
        "501 B3 Town"
    ),
        (
        2,
        sam,
        "sam@xyz.com",
        "502 B3 Town"
    ),
        (
        3,
        jhon,
        "jhon@xyz.com",
        "503 B Town"
    )
)

这是我的viewdidload

- (void)viewDidLoad
{
    [super viewDidLoad];
    statuses=[[NSMutableArray alloc]init];
    NSURL *myURL = [NSURL URLWithString:@"http://url/result.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
         id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"jsonObject=%@",jsonObject);
          statuses = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
 }];

}

现在我想在 uitableview 中显示所有记录。谁能告诉我我该怎么做。谢谢

【问题讨论】:

  • 你知道如何创建UITableView 吗?
  • yupi 确实知道这一点,但我如何将这些数据加载到 uitableview 中,这是我的问题。谢谢
  • 创建一个模型NSObject模型类,并使用这个模型本身展示。
  • 你能分享你的json响应数据吗?

标签: ios iphone objective-c xcode uitableview


【解决方案1】:

查看本教程: http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return myObject.count;
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Item";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell=[[UITableViewCell alloc]initWithStyle:
    UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}


NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

NSMutableString *text;
//text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:title]];
text = [NSMutableString stringWithFormat:@"%@",
       [tmpDict objectForKeyedSubscript:title]];

NSMutableString *detail;
detail = [NSMutableString stringWithFormat:@"Author: %@ ",
         [tmpDict objectForKey:author]];

NSMutableString *images;
images = [NSMutableString stringWithFormat:@"%@ ",
         [tmpDict objectForKey:thumbnail]];

NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];


cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
cell.imageView.image =img;

return cell;
}

【讨论】:

    【解决方案2】:

    I 这个格式是数组 -> 内部数组中的数组格式可以计数是一致的(4)个元素

    试试这个

    Sections 方法中的行数 - jsonparsingarray 计数

    索引方法处的行单元格

    像这样显示例如:索引为 1 和 3 的姓名和电子邮件 ID

    if ([[jsonparsingarray objectatindex:indexpath.row] count])
    {
     cell.textlable.text     = [[jsonparsingarray objectatindex:indexpath.row]objectatindex:1];
     cell.detailtextlable.text = [[jsonparsingarray objectatindex:indexpath.row]objectatindex:3];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多