0

在 UITableViewController 子类中,我无法加载数据,我正在从 Url 获取 json 数据,

json数据:

{
"terms": "a",
"results": {
    "Events": [
        {
            "Event_Name": "808 State",
            "Event_NavigateURL": "808-state"
        },
        {
            "Event_Name": "ASAP Rocky",
            "Event_NavigateURL": "asap-rocky"
        },
        {
            "Event_Name": "Abba Mania",
            "Event_NavigateURL": "abba-mania"
        },
        {
            "Event_Name": "Adam Ant",
            "Event_NavigateURL": "adam-ant"
        }
    ],
    "Sports": [
        {
            "SportsName": "Arsenal",
            "URL": "football/arsenal-tickets.htm",
            "SubCategory_NavigateURL": "football",
            "Event_NavigateURL": "arsenal",
            "type": "team"
        },
        {
            "SportsName": "Aston Villa",
            "URL": "football/aston-villa-tickets.htm",
            "SubCategory_NavigateURL": "football",
            "Event_NavigateURL": "aston-villa",
            "type": "team"
        },
        {
            "SportsName": "Fulham",
            "URL": "football/fulham-tickets.htm",
            "SubCategory_NavigateURL": "football",
            "Event_NavigateURL": "fulham",
            "type": "team"
        },
        {
            "SportsName": "Manchester City",
            "URL": "football/manchester-city-tickets.htm",
            "SubCategory_NavigateURL": "football",
            "Event_NavigateURL": "manchester-city",
            "type": "team"
        }
    ],
    "Venues": [
        {
            "Venue": "O2 Arena London",
            "Venue_LocationURL": "o2-arena-london",
            "Venue_City": "London",
            "Venue_Country": "United Kingdom",
            "Venue_CountryURL": "united-kingdom"
        },
        {
            "Venue": "The Odyssey Arena",
            "Venue_LocationURL": "the-odyssey-arena",
            "Venue_City": "Belfast",
            "Venue_Country": "United Kingdom",
            "Venue_CountryURL": "united-kingdom"
        },
        {
            "Venue": "Manchester Arena",
            "Venue_LocationURL": "manchester-arena",
            "Venue_City": "Manchester",
            "Venue_Country": "United Kingdom",
            "Venue_CountryURL": "united-kingdom"
        },
        {
            "Venue": "SECC Clyde Auditorium",
            "Venue_LocationURL": "secc-clyde-auditorium",
            "Venue_City": "Glasgow",
            "Venue_Country": "United Kingdom",
            "Venue_CountryURL": "united-kingdom"
        }
    ]
}

}

这是我的代码片段,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{              
    return [dict count]; 
}

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

    cell.textLabel.text= [[[dict valueForKey:@"results"] valueForKey:@"Sports"] valueForKey:@"Event_NavigateURL"];

    cell.detailtextLabel.text= [[[dict valueForKey:@"results"] valueForKey:@"Sports"] valueForKey:@"type"];


    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     newViewController *obj=[[newViewController alloc]init];
     obj.str=[[[[dict valueForKey:@"results"] valueForKey:@"SportsName"] valueForKey:@"Event_NavigateURL"] valueForKey:indexPath.row];
     [self.navigationController pushViewController:obj animated:YES];

}

NSDictionary 应该如何与 TableView 一起工作?

完成这项工作的最简单方法是什么?

4

4 回答 4

2

而不是这个

cell.textLabel.text= [[[dict valueForKey:@"results"] valueForKey:@"Sports"] valueForKey:@"Event_NavigateURL"];

    cell.detailtextLabel.text= [[[dict valueForKey:@"results"] valueForKey:@"Sports"] valueForKey:@"type"];

obj.str=[[[[dict valueForKey:@"results"] valueForKey:@"SportsName"] valueForKey:@"Event_NavigateURL"] valueForKey:indexPath.row];

用这个 ..

cell.textLabel.text= [[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"Event_NavigateURL"];

    cell.detailtextLabel.text= [[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"type"];

obj.str=[[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"SportsName"] ;

更新 :

只需在您的 cellForRowIndex 中更新此代码

      cell.textLabel.text= [ NSString stringWithFormat:@"%@ , %@ ,%@ ",
 [[[[dict valueForKey:@"results"] valueForKey:@"Events"] objectAtIndex:indexPath.row] valueForKey:@"Event_Name"] ,
 [[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"SportsName"],
 [[[[dict valueForKey:@"results"] valueForKey:@"Venues"] objectAtIndex:indexPath.row] valueForKey:@"Venue"] ];
于 2013-05-08T12:51:18.783 回答
1

用这个替换你的:

cell.textLabel.text= [[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"Event_NavigateURL"];

cell.detailtextLabel.text= [[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey:@"type"];
于 2014-01-16T17:57:19.747 回答
1

试试这样

 NSMutableArray *array=[[dict valueForKey:@"results"] valueForKey:@"Sports"];

在这里,您将获取数组并在 tableview 方法中使用此数组,如下所示

NSString *str=[[array objectAtIndex:indexPath.row] valueForKey@"Event_NavigateURL"];

EDIT:-

    NSString *sportsName=[[[[dict valueForKey:@"results"] valueForKey:@"Sports"] objectAtIndex:indexPath.row] valueForKey@"SportsName"];
    NSString *Event_Name=[[[[dict valueForKey:@"results"] valueForKey:@"Events"] objectAtIndex:indexPath.row] valueForKey@"Event_Name"];
    NSString *Venues=[[[[dict valueForKey:@"results"] valueForKey:@"Venues"] objectAtIndex:indexPath.row] valueForKey@"Venue"];
于 2013-05-08T12:53:04.650 回答
0

尝试这个:

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  id obj = [statuses objectAtIndex:indexPath.row];
  cell.textLabel.text =[[[obj valueForKey:@"results"] valueForKey:@"Sports"] valueForKey:@"Event_NavigateURL"];
  return cell;
} 

希望能有所帮助。

于 2013-05-08T12:57:31.107 回答