在分段控件中显示 json 数据 table 视图

display json datas table view in segmented control

朋友们, 我需要在 table 查看来自 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;

}

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

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);

     }

好的,

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

在调用 [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 class 中,确保使用分配给它的 cellData 正确绘制标题标签或值标签。

请在编写代码之前阅读 UITableView 文档。

UITableView渲染顺序为

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

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

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

然后我使用下面的代码

显示在table视图中
- (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;
}