2013-05-10 62 views
1
- (void)viewDidLoad { 

self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; 
self.detailView.dataSource = self; 
self.detailView.delegate = self; 
self.detailView.multipleTouchEnabled=YES; 
[self.view addSubview:self.detailView]; 
[super viewDidLoad]; 
self.title = NSLocalizedString(@"Routes", nil); 
self.detailArray = [[NSMutableArray alloc] init]; 
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"]; 
NSURL *googleRequestURL=[NSURL URLWithString:url]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
NSError* error; 
NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data    options:kNilOptions error:&error]; 
NSArray *allkeys = [parsedJson allKeys]; 

for(int i = 0; i < allkeys.count; i++){ 

if([[allkeys objectAtIndex:i] isEqualToString:@"routes"]){ 
NSArray *arr = [parsedJson objectForKey:@"routes"]; 
NSDictionary *dic = [arr objectAtIndex:0]; 
NSArray *legs = [dic objectForKey:@"legs"]; 
for(int i = 0; i < legs.count; i++){ 
NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"]; 
for (int i = 0; i < stepsArr.count; i++) { 
NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]); 
NSLog(@"############################"); 
[self.detailArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ]; 
       } 
      } 

     } 

    }   
});  
} 

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 
return 1; 
} 

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{ 
return [self.detailArray count]; 

} 


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


UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 300.0f, 30.0f)]; 
label.text=[NSString stringWithFormat:@"%@",[self.detailArray objectAtIndex:indexPath.row]]; 
label.numberOfLines = 3; 
label.font = [UIFont fontWithName:@"Helvetica" size:(12.0)]; 
label.lineBreakMode = UILineBreakModeWordWrap; 
label.textAlignment = UITextAlignmentLeft; 
[cell.contentView addSubview:label]; 
[label release]; 

return cell; 
} 

我想在Table View中显示detailArray。但它在表视图中显示空值。但在NSlog中它显示当前值。detailArray具有方向值组。是任何可能在TableView中显示。如果我在displayArray中给出常量值,并在TableView中正确显示它。如果有任何人知道,请帮助我。我在等待您宝贵的答案,谢谢。NSMutableArray在表视图中显示空值

+0

尝试在将值添加到detailArray之后立即添加一个断点,以检查detailArray是否确实具有值。 – Anila 2013-05-10 11:43:48

+0

也为什么你设置委托tableview在'cellForRowAtIndexPath:'?也为什么重载tableview在'cellForRowAtIndexPath:'方法??从'cellForRowAtIndexPath:method'移除这两行tableView.delegate = self;和[self.detailView reloadData]; – 2013-05-10 11:45:40

+0

Anila detail数组数值校正。 – 2013-05-10 11:53:08

回答

1

修订

添加后在后整体数据(值)保留并重新载入的TableView像波纹管......

[self.detailArray retain]; 
[self.detailView reloadData]; 

,我相信你的问题解决了... :)

+0

我删除那两行,但没有change.please帮助我。 – 2013-05-10 11:59:11

+0

set label.textColor = [UIColor blackColor];并设置文本每个标签的测试尝试它 – 2013-05-10 12:04:15

+0

我添加了颜色,但仍然显示空值。 – 2013-05-10 12:19:49

1

cellForRowAtIndexPath中删除[self.detailView reloadData];方法。并以同样的方法删除tableView.delegate = self;

并记住[super viewDidLoad];必须写在viewDidLoad的顶部。

编辑 只需更改您的四个数组,如下所示。

NSArray *legs=(NSArray *)[dic objectForKey:@"legs"]; 
    NSArray *arr = (NSArray *)[parsedJson objectForKey:@"routes"]; 
    NSArray *legs = (NSArray *)[dic objectForKey:@"legs"]; 
    NSArray *stepsArr = (NSArray *)[[legs objectAtIndex:i] objectForKey:@"steps"]; 
+0

我删除那两行,但没有change.please帮助我。 – 2013-05-10 11:58:28

+0

print parsedJson打印什么? – 2013-05-10 12:06:14

+0

它显示两条路线之间的全部细节.eg:(lat,long,dis,route).i从中提取路线值并详细存储数组。 – 2013-05-10 12:15:04

0

为什么在viewDidLoad中设置detailView委托和数据源2次?请首先格式化代码,然后我们可以很容易地描述问题。

self.detailView.dataSource=self; 
self.detailView.delegate=self; 
self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; 
self.detailView.dataSource = self; 
self.detailView.delegate = self;