【问题标题】:display json datas table view in segmented control在分段控件中显示 json 数据表视图
【发布时间】:2015-07-07 06:16:53
【问题描述】:

朋友们, 我需要在 json 服务的表格视图数据中显示详细信息列表,我尝试了下面的代码,但它不起作用

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"unrealized";

    samplecell *cell = (samplecell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"unrealized" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

else if(mySegmentedcontrol.selectedSegmentIndex==3)
{
    settings.hidden=YES;
    connection.hidden=YES;
    openTrades.hidden=YES;
    closedTrades.hidden=NO;
    NSString *link=[NSString stringWithFormat:@"http://www.dummydata.com/MMWS/Client/ClosedTrades.ashx?ClientId=%@",clientId];
    NSURL *URLGet=[NSURL URLWithString:link];
    NSData *data=[NSData dataWithContentsOfURL:URLGet];
    NSError *error;
    jsonReturnArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSLog(@"json%@",jsonReturnArray);
    cell.unRealizedProfit.text=[[jsonReturnArray valueForKey:@"Profit"]objectAtIndex:0];
    cell.strategy.text=[[jsonReturnArray valueForKey:@"StrategyName"]objectAtIndex:0];
    NSString *totalProfit=[[jsonReturnArray valueForKey:@"TotalProfit"]objectAtIndex:0];
    closedTotalUnrealizedProfit.text=totalProfit;
}



    NSString *cellValue = [jsonReturnArray objectAtIndex:indexPath.row];
    cell.unRealizedProfit.text=cellValue;
    cell.unRealizedProfit.text = [jsonReturnArray valueForKey:@"Profit"];
    cell.strategy.text =[jsonReturnArray valueForKey:@"StrategyName"];

    return cell;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"json%@",jsonReturnArray);
    return jsonReturnArray.count;

}

我也试过代码,不知道如何在分段控件中引入 tableview 属性

else if(mySegmentedcontrol.selectedSegmentIndex==3)
     {
         settings.hidden=YES;
         connection.hidden=YES;
         openTrades.hidden=YES;
         closedTrades.hidden=NO;
         strategyClosed.hidden=NO;
         NSString *link=[NSString stringWithFormat:@"http://www.dummmydata.com/MMWS/Client/ClosedTrades.ashx?ClientId=%@",clientId];
         NSURL *URLGet=[NSURL URLWithString:link];
         NSData *data=[NSData dataWithContentsOfURL:URLGet];
         NSError *error;
         jsonReturnArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
         NSLog(@"json%@",jsonReturnArray);

     }

【问题讨论】:

  • 什么不完全有效?你不应该在“if (cell == nil)”之后使用“else if”,因为它说:“如果单元格不存在,我们创建它”,如果单元格存在,我们用它填充信息”。但在单元格不存在的情况下,你永远不会填写它。
  • 我需要在表格视图中显示这些数据,并且数据来自 json 服务我是这个编码的新手,我试图在标签中显示这些数据,但它有效。我不知道如何在表格视图中显示请参考我一些代码
  • 我不知道如何在段控件中引入表格视图属性,即,我不知道如何使用段控件显示特定视图并将数据作为表格视图获取,请指导我
  • 那我建议先了解UISegmentedControl的工作原理,UITableView的工作原理,然后结合起来。
  • 我知道表格视图是如何工作和自定义的。问题是不知道如何在段控制中引入表格视图

标签: ios iphone json uitableview uisegmentedcontrol


【解决方案1】:

好的,

您的数据源不应在函数 cellForRowAtIndexPath 中获取,CellForRowAtIndexPath 用于根据 tableView 的 dataSource 显示行的每个单元格。显然,当每个单元格都在渲染时,您不应该获取数据源。

在调用 [myTableView reloadData] 之前,您应该始终确保您的 tableView 的数据源已准备好;

解决方案:在您的 viewDidAppear 功能块中,将您的逻辑编写如下

if(mySegmentedcontrol.selectedSegmentIndex==3){
    NSString *link=[NSString stringWithFormat:@"http://www.dummydata.com/MMWS/Client/ClosedTrades.ashx?ClientId=%@",clientId];
    NSURL *URLGet=[NSURL URLWithString:link];
    NSData *data=[NSData dataWithContentsOfURL:URLGet];
    NSError *error;
    self.jsonDataSource = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    [self.thisTableView reloadData];
}

然后在你的 CellForRowAtIndexPath 中,像下面那样做正常的事情

NSDictionary *objCellData = [self.jsonDataSource objectAtIndex:indexPath.row];

cell.cellData = objCellData;
return cell;

然后,在您的 Cell 类中,确保使用分配给它的 cellData 正确绘制标题标签或值标签。

编码前请阅读 UITableView 文档。

UITableView的渲染顺序是

  1. 数据源已准备就绪
  2. 调用[tableView reloadData];
  3. UITableView dataSource/delegate 函数将被触发,例如 numberOfRowsInSection、heightForRowAtIndexPath、cellForRowAtIndexPath 等。

【讨论】:

  • cell.cellData = objCellData;返回单元格;如何在单元格中分配的标签中显示值,请详细解释。如何在上面的代码中从数组(即来自json)中获取值来标记
  • 在您的代码中,我看到您具有以下属性,这些属性应该是用于显示这些信息的 UILabel “unRealizedProfit”、“strategy”、“closedTotalUnrealizedProfit” 所以在您的 UITableViewCell 类中,只需使用你定义的那些标签。 self.unRealizedProfit.text = [self.objCellData objectForKey:@"Profit"];
  • 我不确定您的数据源格式如何。但是如果这个 self.objCellData 像 @{@"StrategyName": @"Butterfly", @"Profit":@50},那么它应该像上面那样工作。
  • 当我选择我只有空表视图的那个段时它不起作用
  • 请按顺序检查以下内容。 1.如果你的dataSource在你做[tableView reloadData]之前就准备好了,你的dataSource格式是什么。 2.当分配cell.cellData = objCellData时,你的objCellData的格式是什么。 3.在你的单元格类中,你如何根据给定的cellData渲染标签的文本,你是调用[self setNeedsDisplay]还是通过cellData强制单元格重绘内容?请先阅读 UITableView 的示例,尝试了解整个过程。 developer.apple.com/library/ios/samplecode/TableViewSuite/…
【解决方案2】:

最后我得到了答案,我自定义了单元格并使用下面的代码根据数组的计数分配行数

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return jsonReturnArray.count;
}

然后我使用下面的代码显示在表格视图中

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

    samplecell *cell = (samplecell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    //loads two table view for  open and closed trades with reference of client id
    if (cell == nil) 
    {
        //table view for closed trades
        if (tableView==strategyClosed) 
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"samplecell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            NSString *link=[NSString stringWithFormat:@"http://www.managedtradingsystems.com/MMWS/Client/ClosedTrades.ashx?ClientId=%@",clientId];
            NSURL *URLGet=[NSURL URLWithString:link];
            NSData *data=[NSData dataWithContentsOfURL:URLGet];
            NSError *error;
            jsonReturnArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            //checks whether datas are present in array or array and datas present are null and displays datas present through labels
             if (![jsonReturnArray isEqual:[NSNull null]])
             {
            NSString *strategy=[[jsonReturnArray valueForKey:@"StrategyName"]objectAtIndex:indexPath.row];
            if (![strategy isEqual:[NSNull null]]) 
            {

            cell.strategy.text=strategy;
            }
            else
            {
                cell.strategy.text=@"";
            }

下面代码使用数组计数的行数

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

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多