1

我希望为 ios 创建一个基本程序,该程序在一个视图中使用数据数组和 2 个 UITableView。我希望第二个 UITableView 由一个数组填充,该数组基于第一个表中所做的选择。我有第二个表的数组数组。当我单击 table1 的第一个单元格时,所需的数据将显示在 table2 中。但是当我单击 table1 的第二个单元格时,我得到了错误。“索引 1 越界”

-(void)viewWillAppear:(BOOL)animated{
 [super viewWillAppear:animated];
 NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http:/url.com/technology"] encoding:NSStringEncodingConversionAllowLossy error:nil];
NSString *jsonString1 = [NSString stringWithContentsOfURL:[NSURL  URLWithString:@"http://url.com/experience1"] encoding:NSStringEncodingConversionAllowLossy error:nil];
NSString *jsonString2 = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http:url.com/experirnce2"] encoding:NSStringEncodingConversionAllowLossy error:nil];

SBJSON *parser = [[SBJSON alloc]init];
SBJSON *parser1 = [[SBJSON alloc]init];
SBJSON *parser2 = [[SBJSON alloc]init];

 NSDictionary *results = [parser objectWithString:jsonString error:nil];
[parser release], parser = nil;
    NSDictionary *results1 = [parser1 objectWithString:jsonString1 error:nil];
    NSDictionary *results2 = [parser2 objectWithString:jsonString2 error:nil];
[parser release], parser = nil;
[parser1 release], parser1 = nil;
[parser2 release], parser2 = nil;

self.technologyArray = [results objectForKey:@"response"];
self.technologyData = [technologyArray valueForKey:@"technologyname"];
self.experienceArray1 = [results1 objectForKey:@"response"];
self.experienceData1 = [experienceArray1 valueForKey:@"experiencename"];
self.experienceArray2 = [results2 objectForKey:@"response"];
self.experienceData2 = [experienceArray2 valueForKey:@"experiencename"];
experience = [[NSMutableArray alloc]initWithObjects:experienceData1, experienceData2, nil];


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == technologyTable)
return [technologyData count];

else {
return [experience count];
}

 }



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


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Change UITableViewCellStyle
cell = [[[UITableViewCell alloc] 
     initWithStyle:UITableViewCellStyleSubtitle 
     reuseIdentifier:CellIdentifier] autorelease];
 }

if (tableView == technologyTable)
{

cell.textLabel.text=[technologyData objectAtIndex:indexPath.row];

} 

if (tableView == experienceTable)
{
 cell.textLabel.text = [[experience  objectAtIndex:indexPath.row]objectAtIndex:indexPath.row];
[experienceTable reloadData];

}
return cell ;

}

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

experienceTable = [[experience objectAtIndex:indexPath.row] objectAtIndex:indexPath.row];


 selectTechnology.text = [technologyData objectAtIndex:indexPath.row];

 self.technologyList.hidden = YES;

}
 if(tableView == experienceTable)
 {
 self.experienceList.hidden = YES;
 selectExperience.text = [[experience objectAtIndex:1] objectAtIndex:indexPath.row];
 }
 [experience release ];

  }
4

2 回答 2

0

我将为 2 个表使用 2 个不同的委托和数据源。在第一个表中选择后,其代表将发送第二个表的代表将监听的通知。当第二个表的代理收到通知时,它会设置自己显示适当的数据(经验数组和数据 1 或 2),然后调用[table2 reloadData].

于 2012-11-03T15:06:40.323 回答
0

cellForRowAtIndexPath您需要确定在第一个 tableview 中选择了哪一行(您可以将选择保存在 ivar 中),然后在第二个 tableview 中显示相应数组中的值。在用户从第一个表中选择一个值后,您需要调用 [experienceTable reloadData]。

像这样的东西:

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (tableView == technologyTable)
    {
        selectedTechnology = indexPath.row;
        [experienceTable reloadData];
    }
}

接着:

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


    if (tableView == experienceTable)
    {
        cell.textLabel.text = experience[selectedTechnology][indexPath.row];
    }
}
于 2012-11-03T15:08:32.623 回答