Table view inside a normal view controller

2019-05-22 15:20发布

I'm doing this iPhone project where I need to have a (dynamic) table view inside a normal view controller. I didn't choose a Table View Controller because I need to put other stuff in the page. Imagine a page with text fields, buttons and my small table view of about 4-5 cells.

When I run the app, I need to touch a button to segue to this view. The I click the button, the app crashes and tells me that:

2012-07-22 14:40:57.304 How Many?[3055:f803] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061

This is my .H file:

#import <UIKit/UIKit.h>

@interface ProjectViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

This is my .M file:

#import "ProjectViewController.h"

@interface ProjectViewController ()

@end

@implementation ProjectViewController


//MyViewController.m
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    NSLog(@"cellForRowAtIndexPath");

    cell.textLabel.text = @"Test";

    return cell;
}


@end

I control-dragged from the table view to my controller to set the delegate and the datasource.

What did I do wrong??

Thanks for your help.

2条回答
甜甜的少女心
2楼-- · 2019-05-22 15:34

In your -cellForRowAtIndexPath, if you do this, you will be fine.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"cell"];
}

NSLog(@"cellForRowAtIndexPath");

cell.textLabel.text = @"Test";

return cell;

}

The problem is your cell never got memory allocated and initialized. That's why you get the error.

查看更多
淡お忘
3楼-- · 2019-05-22 15:45

Try to make an outlet in your .h file and wire it to the tableView in your storyboard

ProjectViewController.h

@property (nonatomic, strong) IBOutlet UITableView *myTableView;

ProjectViewController.m

@synthesize myTableView;

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");

    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"cell"];

    NSLog(@"cellForRowAtIndexPath");

cell.textLabel.text = @"Test";

return cell;

}

查看更多
登录 后发表回答