2012-11-03 51 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',這會導致它無限期地運行。從那裏刪除並嘗試我的方法,事情應該解決。 –

相關問題