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

时间:2015-07-07 06:16:54

标签: ios iphone json uitableview uisegmentedcontrol

i need to display in table view as in image

朋友, 我需要在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;

}

我也尝试了代码,并且不知道如何在seegmented控件中引入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);

     }

2 个答案:

答案 0 :(得分:0)

好的,

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

在调用[myTableView reloadData]之前,您应该始终确保tableView的dataSource已准备就绪;

解决方案:在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. DataSource准备就绪
  2. 调用[tableView reloadData];
  3. 将触发UITableView dataSource / delegate函数,如numberOfRowsInSection,heightForRowAtIndexPath,cellForRowAtIndexPath等。

答案 1 :(得分:0)

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

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