2012-03-20 85 views
1

我是Xcode和Objective-C編程的新手,需要一些幫助。xcode陣列和UITableViews陣列

我期待爲使用分層數據和2個獨立的UITableViews的iOS創建基本程序。我希望第二個UITableView由在viewControllers之間傳遞的數組填充,基於在第一個UITableView中選擇哪個單元格/行。

程序編譯但運行程序時出現SIGABRT錯誤。有人可以幫我修復SIGABRT並將mainArray傳遞給第二個tableView

這是我得到了多少。

我的代碼:

ArrayTableViewController.h

@interface arrayTableViewController : UITableViewController 
@property (nonatomic, strong) NSMutableArray *mainArray; 
@property (nonatomic, strong) NSMutableArray *secondArray; 
@property (nonatomic, strong) NSMutableArray *thirdArray; 
@end 

ArrayTableViewController.m

#import "ArrayTableViewController.h" 
#import "table2.h" 
@implementation arrayTableViewController 
@synthesize mainArray, secondArray, thirdArray; 

-(void) viewDidLoad { 
[super viewDidLoad]; 
mainArray = [[NSMutableArray alloc] initWithObjects: secondArray,  thirdArray, nil]; 
secondArray = [[NSMutableArray alloc] initWithObjects: @"123", @"456",  nil]; 
thirdArray = [[NSMutableArray alloc] initWithObjects: @"78", @"90", nil]; 
} 



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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [mainArray 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]; 
} 


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

return cell; 
} 

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

table2 *table2Controller = [[table2 alloc] initWithNibName:@"table2" bundle:nil]; 
table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1]; 
[self.navigationController pushViewController:table2Controller animated:YES]; 

} 

@end 

table2.h

#import <UIKit/UIKit.h> 
@interface table2 : UITableViewController 
@property (nonatomic, strong) NSArray *arrayForDisplay; 
@end 

table2.m

@implementation table3 
@synthesize arrayForDisplay; 

然後就是在ArrayTableViewController.m

編輯相同的電池配置風格:

進行必要的更改,當我運行後程序並選擇一行,我在下面一行中得到一個SIGABRT錯誤。

int main(int argc, char *argv[]) 
{ 
@autoreleasepool { 
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([ArrayTableAppDelegate class])); 
} 
} 

您會推薦什麼?我應該關閉ARC並致電我自己的版本?這樣我可以得到第二個tableView

+0

如果您在選擇一行後出現崩潰,那麼這意味着你在'didSelectRowAtIndexPath:'方法中犯了一些錯誤。你可以嗎?顯示代碼? – Ilanchezhian 2012-03-22 03:25:55

+0

didSelectRowAtIndexPath:方法已編輯爲當前代碼 – Webby 2012-03-22 17:31:41

+0

請參閱我的Edit 2 for'didSelectRowAtIndexPath'。 – Ilanchezhian 2012-03-23 05:55:39

回答

3

的第一個錯誤:

viewDidLoad方法,您已經創建了mainArraysecondArraythirdArray爲您分配這些陣列甚至之前的元素。

第二個錯誤:

cellForRowAtIndexPath:方法:

檢查線cell.textLabel.text = [mainArray objectAtIndex:[indexPath row]];

其實textLabel需要一個NSString的值來設置。但是你正在設置一個數組。

編輯:

將值設置爲爲textLabel如下:

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

實際上這將設置該陣列的第一個值。但這取決於你的要求。

編輯2:

arrayForDisplay是一個數組變量,但你是在語句的字符串變量設置到

table2Controller.arrayForDisplay = [[mainArray objectAtIndex: [indexPath row]] objectAtIndex:1]; 

做它,如下所示:

table2Controller.arrayForDisplay = [mainArray objectAtIndex: [indexPath row]]; 
+0

它對你有幫助嗎? – Ilanchezhian 2012-03-20 05:03:41

+0

是的,它適用於單元配置的例外情況。我將如何配置tableCell以顯示數組的項目? – Webby 2012-03-20 19:50:49

+0

我已經更新了我的答案。 PL。檢查一下。 – Ilanchezhian 2012-03-21 04:27:35