3

I'm trying to add a UITableView to an existing UIViewController programmatically.

My run.h file has this:

@interface runTests : UIViewController <UINavigationControllerDelegate,UITextFieldDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource> {NSTimer *Timer;}

@property (strong, nonatomic) UITableView *tableView;

My app runs through a speed test and at the end of the test I'm trying to add a TableView to the View. At the moment I have this:

-(void)addSpeedTable {


    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 45;
    self.tableView.sectionFooterHeight = 22;
    self.tableView.sectionHeaderHeight = 22;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];


}

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

    {
        return 1;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"newCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }

        cell.textLabel.text = @"Testing";

        return cell;
    }

The app never makes it into the cellForRowAtIndexPath method but does make it into the other two above. I can't see any error in the Xcode output.

Anything I need to be doing here:

enter image description here

When the app fails all I get is this:

enter image description here

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Dan
  • 2,304
  • 6
  • 42
  • 69
  • You can catch the actual exception by adding an exception breakpoint - quick guide here: http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions – Gavin Bunney Mar 04 '15 at 01:26
  • I've done this yet I still only get this. I set a breakpoint on "return 1;" for numberOfRowsInSection. The app gets to this breakpoint. When I continue it fails and gives the screenshot above – Dan Mar 04 '15 at 01:32
  • Make sure you add a breakpoint for `All Exceptions` - it should then break on whatever line is causing the `EXC_BAD_ACCESS` to occur :) – Gavin Bunney Mar 04 '15 at 01:40
  • I tried this @GavinBunney but it didn't break, it just ended on the same screen as before – Dan Mar 04 '15 at 03:08

6 Answers6

6

If width of UITableView is zero, it will never call datasource

So change your code like this

self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

Good luck!

Tony
  • 4,311
  • 26
  • 49
  • I've added this with no luck, still crashes – Dan Mar 04 '15 at 01:10
  • 1
    @DanJamesPalmer It crashes? You never said that in your question. – NobodyNada Mar 04 '15 at 01:15
  • I said it "never made it to the CFRAIP", which I guess was my way of saying it crashed. I'll add the view I get when it fails – Dan Mar 04 '15 at 01:23
  • @DanJamesPalmer: let catch all exception to find out where carsh is. The above code is so simple, it can make crash – Tony Mar 04 '15 at 04:08
  • I have "catch all exceptions" turned on but it doesn't catch any. very frustrating – Dan Mar 04 '15 at 19:46
  • @DanJamesPalmer: really hard to know what happened with your code. Please make it more clear, show full code, it will make everyone understand flow of your code and help you – Tony Mar 05 '15 at 02:35
  • @VietHung I did a ton more testing last night and concluded, rather embarrassingly, that it had NOTHING to do with the addSubView code :( – Dan Mar 05 '15 at 20:17
2

You should init your UITableView with a frame (and optionally a style), or at least define the frame somewhere.

For example:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
BorisE
  • 385
  • 2
  • 7
1

You need to assign the property tableView to the instance you are creating in the addSpeedTable method, then reference it using self.tableView.

-(void)addSpeedTable {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    self.tableView.rowHeight = 45;
    self.tableView.sectionFooterHeight = 22;
    self.tableView.sectionHeaderHeight = 22;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.view addSubview:self.tableView];
}

Edit

As pointed out by the other answers, you'll probably need to set the frame on the UITableView as well:

self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
Gavin Bunney
  • 1,240
  • 1
  • 12
  • 12
  • Hi Gavin, I've tried this with no luck! Any idea how I can get more details on the error? The lack of Xcode feedback is frustrating – Dan Mar 04 '15 at 01:07
  • try adding [self.tableview reloadData]; to the end. – Earl Grey Mar 04 '15 at 01:08
  • I've updated the OP with your changes and this still isn't working for me. I did try the reloadData, to no avail – Dan Mar 04 '15 at 01:14
  • Can you add your entire ViewController implementation file? Wondering if the timing of the `self.view` might be nil when you are trying to add the `tableView` to it – Gavin Bunney Mar 04 '15 at 01:21
  • You're right, I think it is related to the self.view @GavinBunney. I added an property of a UIView and initialized it at the beginning of my code. It didn't fail but the TableView took up the entire screen from the first second (covered my existing view. I don't want the TableView to appear until I call it – Dan Mar 04 '15 at 03:53
1

You use init method to initialize a view, which means the view's frame is CGRectZero. So the numberOfRowsInSection is called but SDK finds no cell need to be shown as the frame is CGRectZero.

-(void)addSpeedTable {

.....

tableView.frame = self.view.bounds;
[self.view addSubview:tableView];

}

What's more you should assign your tableView to the property by self.tableView = tableView or your tableView property will never be valid state.

user3349433
  • 480
  • 2
  • 11
  • I've added the "init with frame" code by @VietHung but this makes no difference – Dan Mar 04 '15 at 01:11
  • Is my .h file correct? I haven't synthesized the "tableView" from the .h file – Dan Mar 04 '15 at 01:16
  • You need to register the @"newCell" to your tableview when it's initialized with [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"]. – user3349433 Mar 04 '15 at 02:59
0

You can add breakpoint at the thirdLine, and print out the frame see if one of its bounds is 0.

0

I think you may have declare the wrong syntax. I can't upload image, So notice the difference the code below. The capitalization may be the problem. { // 服务条款 static NSString * cellId = @"justCommon";

    static NSString *CellIdentifier = @"newCell";
}