我是一名新的iPhone程序员。我正在制作基于导航的应用程序。其中一个视图包含一个tableview。我成功地制作了一个单视图应用程序,在视图中使用带有开关的tableview,但是当我在基于导航的应用程序中尝试代码时,它崩溃了: 线程1:编程接收信号:“SIGABRT”。
这是我的代码: FourthViewController.h
#import <UIKit/UIKit.h>
@interface FourthViewController : UIViewController {
NSArray *contentArray;
}
@property (strong, nonatomic) IBOutlet UITableView *table;
- (void) switchChanged;
@end
FourthViewController.m
@implementation FourthViewController
@synthesize table;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
contentArray = [NSArray arrayWithObjects:
@"One",
@"Two",
@"Three", nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTable:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
//Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return 0;
return [contentArray 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] ;
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
[switchview setOn:YES animated:NO];
NSInteger row;
row = [indexPath row];
switchview.tag = row;
[switchview addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchview;
}
cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}
- (void) switchChanged:(id)sender{
UISwitch *switchview = sender;
if (switchview.tag == 0) {
NSLog(@"First");
}
}
@end
FourthViewController.xib包含: TableView连接到表属性 数据源和委托设置为文件所有者。
我做错了什么?
答案 0 :(得分:1)
我认为您需要在文件所有者中连接您的IBOutlet表。
或者
请改变如下。
#import <UIKit/UIKit.h>
@interface FourthViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>
{
NSArray *contentArray;
}
@property (strong, nonatomic) IBOutlet UITableView *table;
- (void) switchChanged;
@end
答案 1 :(得分:0)
该错误与表格视图无关