我是Xcode和Objective-C编程的新手,需要一些帮助。
我希望为iOS创建一个使用层次结构数据和两个单独的UITableViews
的基本程序。我希望第二个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
表2.h
#import <UIKit/UIKit.h>
@interface table2 : UITableViewController
@property (nonatomic, strong) NSArray *arrayForDisplay;
@end
表2.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
?
第一个错误:
在viewDidLoad
方法中,甚至在分配这些数组之前,就已经创建了以secondArray
和thirdArray
为元素的mainArray
。
第二个错误:
在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]];