Question

I have created a UIViewController that contains a UIView, UIToolbar and a UITableView. This is what I have done to create the View.

  • hooked up the elements to IBOutlets in Interfacebuilder
  • Called UITableViewDelegate and DataSource in the class header

and then I have tried to set the view up in ViewDidLoad like this.

- (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];

}

I have also made sure that I have the DataSource and tableView Delegate set in this class

- (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

This is what it says when it crashes from me trying to scroll the UITableView

Thread 1:EXC_BAD_ACCESS(code=1, address=0x415dc466)'

Nothing is being output to the console, so finding it hard to figure out what this error means.

Update 1: cellForRowAtIndexPath method

- (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;
}

Update 2: Stack trace

It appears any interaction with any of the UIButtons in the toolbar or the view causes the error. Below is the stack trace.

<_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
)

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top