2012-11-03 18 views
1

我期待为在一个视图中使用数组数据和2个UITableViews的ios创建一个基本程序。我希望第二个UITableView由一个基于第一个表中所做选择的数组填充。我有第二个表的数组数组。当我点击table1的第一个单元格时,所需数据显示在表2中。但是当我点击table1的第二个单元格时出现错误。 “Index 1 beyond bounds”更新第二个表视图关于第一个作出的选择

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

    } 

回答

0

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

+0

能否请您解释一下我的代码。我已经尝试过,但我再次得到相同的错误...索引1超越界限。这里的体验是两个数组的数组。 –

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]; 
    } 
} 
+0

我试了一下。但是,每当我把重载数据放在didSelectRowAtIndexPath中时,我就会得到SIGABRT错误。 –

+0

我的猜测是你收到的SIGABRT是由于无限循环。目前你在'cellForRowAtIndexPath'中使用'reloadData',这会导致它无限期地运行。从那里删除并尝试我的方法,事情应该解决。 –

相关问题