2

表视图:

-(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];
}
tableView.delegate = self;
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];
[self.myTableView reloadData];

return cell;

我想将详细信息(数组值)存储在tableView。我使用了这两种方法numberOfRowsInSectioncellForRowAtIndexPath但表显示为空。如何显示?

4

4 回答 4

1

您的代码:

NSString  *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
    NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    // NSLog(@"data:%@",someString);

    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"]){
            arr  = [parsedJson objectForKey:@"routes"];
            dic   = [arr objectAtIndex:0];
            //  NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
            legs = [dic objectForKey:@"legs"];
            // NSLog(@"legs array count %d", legs.count);
            for(int i = 0;  i < legs.count; i++){
                stepsArr = [[legs objectAtIndex:i] objectForKey:@"steps"];
                for (int i = 0; i < stepsArr.count; i++) {
                    NSLog(@"HTML INSTRUCTION %@", [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"]);
                    NSLog(@"############################");
                    NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;
                }
            }

        }

    }        
});

首先你去你的 .h 文件 -->

@property (nonatomic, strong) NSMutableArray * detailsArray;

在 .m 文件中 -->

@synthesize detailsArray;

替换此代码

NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

通过这个 -->

self.detailsArray = [[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"];


# using table datasource methods 

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


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
 }

cell.titleLabel.text = [self.detailsArray objectAtIndex: indexPath.row ];

 return cell;
}

}

//注意请设置table的委托方法,如果是编程方式写代码意味着 tableView.delgate = self;

用这个 :

self.detaisArray = [[NSMutableArray alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
    NSString  *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%f,%f&sensor=false",startPoint,midannotation.coordinate.latitude,midannotation.coordinate.longitude];
    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];

        // NSLog(@"data:%@",someString);

        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];
                //  NSLog(@"ALL KEYS FROM ROUTE: %@", [dic allKeys]);
                NSArray *legs = [dic objectForKey:@"legs"];
                // NSLog(@"legs array count %d", legs.count);
                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.detaisArray addObject:[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ];
                        if(i == legs.count-1){
                            self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 400) style:UITableViewStylePlain];
                            self.myTableView.delegate = self;
                             self.myTableView.dataSource = self;
                            [self.view addSubview:self.myTableView];
                        }
                    }
                }

            }

        }        
    });
于 2013-05-09T08:57:09.803 回答
1

在 .h 中为 NSMutable 数组创建属性

@property (nonatomic,retain) NSMutableArray *detailArray;

并以 .m 合成

@synthesize detailArray=_detailArray;

并更改此行 NSMutableArray *detail=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

 _detailArray=[[stepsArr objectAtIndex:i] objectForKey:@"html_instructions"] ;

然后使用 _detailArray 来显示表格数据。

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


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// _detailArray objects for date population.
}
于 2013-05-09T07:17:02.593 回答
0

首先检查你连接你的tableview委托集自我......

第二次检查你的行数 pr detailarray cont in numberOfRowsInSection 它可能为零

于 2013-05-09T07:36:15.390 回答
0

第一次在 .h 中声明你的数组

像这样

@property(strong,nonatomic)NSMutableArray * name;

并初始化为 .m Init() 方法

name=[NSMutableArray alloc]initWithObjects:@"a",@"b", nil];  

// TableView 委托方法将数组数据显示到 Tableview 中。

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

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
                return name.count;    //Give Your Array Name Here.  
        }

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

            cell.textLabel.text=[NSString stringWithFormat:@"%@",[name objectAtIndex:indexPath.row]];



            return cell;
        }

试试这个代码。

于 2013-05-09T08:10:27.350 回答