我创建了一个包含UIView,UIToolbar和UITableView的UIViewController。 这就是我创建视图所做的。
然后我试图像这样在ViewDidLoad中设置视图。
- (void)viewDidLoad
{
[super viewDidLoad];
colorController = [[ColorController alloc] init];
[colorController readColorPlist];
//create toolbar
myToolBar = [[UIToolbar alloc] init];
[myToolBar setTranslucent:NO];
myToolBar.frame = CGRectMake(0.0, 0.0, 320.0, 45.0);
[[UIToolbar appearance] setBarTintColor:[UIColor colorWithRed:colorController.oRed/255.0 green:colorController.oGreen/255.0 blue:colorController.oBlue/255.0 alpha:1.0]];
// add title to toolBar
UILabel *toolBarLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 110.0) / 2, 3.0, 110.0, 40.0)];
toolBarLabel.font = [UIFont boldSystemFontOfSize:16];
toolBarLabel.textAlignment = NSTextAlignmentCenter;
toolBarLabel.textColor = [UIColor whiteColor];
toolBarLabel.backgroundColor = [UIColor clearColor];
toolBarLabel.text = @"My View";
// create TableView
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 45.0, self.view.frame.size.width, self.view.frame.size.width - 65)];
[myTableView setDelegate:self];
[myTableView setDataSource:self];
// createButtons
// create an array for the buttons
NSMutableArray* barButtonsArray = [[NSMutableArray alloc] initWithCapacity:5];
// create a Cancel button
UIBarButtonItem * cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancelUIButton)];
cancelButton.style = UIBarButtonItemStyleBordered;
cancelButton.tintColor = [UIColor whiteColor];
[barButtonsArray addObject:cancelButton];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barButtonsArray addObject:flexible];
// create a submitButton
UIBarButtonItem *submitButton = [[UIBarButtonItem alloc]initWithTitle:@"Submit"
style:UIBarButtonItemStylePlain
target:self
action:@selector(submitButton)];
submitButton.style = UIBarButtonItemStyleBordered;
submitButton.tintColor = [UIColor whiteColor];
[barButtonsArray addObject:submitButton];
// put the BarbuttonsArray in the toolbar and release them
[myToolBar setItems:barButtonsArray animated:NO];
// add the views
[self.view addSubview:myTableView];
[self.view addSubview:myToolBar];
[myToolBar addSubview:toolBarLabel];
}
我还确保在此类中设置了DataSource和tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
这就是我试图滚动UITableView
时崩溃的原因Thread 1:EXC_BAD_ACCESS(code=1, address=0x415dc466)'
没有任何内容输出到控制台,所以很难弄清楚这个错误意味着什么。
- (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];
}
// Configure the cell...
return cell;
}
似乎与工具栏中的任何UIButton或视图的任何交互都会导致错误。下面是堆栈跟踪。
<_NSCallStackArray 0x14ebf7b0>(
0 ??? 0x028d1654 0x0 + 42800724,
1 HelloWorld 0x00025f99 main + 0,
2 UIKit 0x2ff0bf7b <redacted> + 266,
3 UIKit 0x2fefa739 <redacted> + 196,
4 UIKit 0x2fda518b <redacted> + 1138,
5 UIKit 0x30130d4f <redacted> + 46,
6 UIKit 0x2fd6c5cf <redacted> + 218,
7 UIKit 0x2fd6ad33 <redacted> + 298,
8 UIKit 0x2fda39fd <redacted> + 772,
9 UIKit 0x2fda33ab <redacted> + 666,
10 UIKit 0x2fd78d79 <redacted> + 196,
11 UIKit 0x2fd77569 <redacted> + 7116,
12 CoreFoundation 0x2d5baf1f <redacted> + 14,
13 CoreFoundation 0x2d5ba3e7 <redacted> + 206,
14 CoreFoundation 0x2d5b8bd7 <redacted> + 630,
15 CoreFoundation 0x2d523471 CFRunLoopRunSpecific + 524,
16 CoreFoundation 0x2d523253 CFRunLoopRunInMode + 106,
17 GraphicsServices 0x322572eb GSEventRunModal + 138,
18 UIKit 0x2fdd8845 UIApplicationMain + 1136,
19 HelloWorld 0x0002600d main + 116,
20 libdyld.dylib 0x37e49ab7 <redacted> + 2
)