I have created .h and .m files for UITableView
called mainTableViewgm.h
and mainTableViewgm.m
resp. and I am calling - initWithFrame
: method from my main view controller to this mainTableViewgm.m
implementation file
[[mainTableViewgm alloc]initWithFrame:tableViewOne.frame]
Note that this tableview is in my main view controller. But I have created separate files for the tableView and have also set the custom class to mainTableViewgm
in storyboard.
the -initWithFrame:
methods appears as follows
- (id)initWithFrame:(CGRect)frame
{
//NSLog(@"kource data");
self = [super initWithFrame:frame];
if (self)
{
[self setDelegate:self];
[self setDataSource:self];
[self tableView:self cellForRowAtIndexPath:0];
[self tableView:self numberOfRowsInSection:1];
// Initialization code
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"kource data");
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"kource data2");
UITableViewCell*cellOne =[[UITableViewCell alloc]init];
cellOne.detailTextLabel.text=@"text did appear";
return cellOne;
}
the -initWithFrame:
is being called fine along with the 'if (self)' block in this method. But the problem is numberOfRowsInSection:
and cellForRowAtIndexPath:
are not being automatically called here . kource data
/ kource data2
never appear in log. What do I do to load the table? Are the delegate
/ datasource
being set incorrectly?
I must mention that I have also set the UITableViewDelegate
and UITableviewDataSource
protocols:
@interface mainTableViewgm : UITableView <UITableViewDelegate,UITableViewDataSource>
@end
Help will be much appreciated. Thank you.
Your tableview is not loaded when the controller is initializing, so you cannot do that in the init
methods. You have to move your code to the viewDidLoad
method.
Also you are not setting the delegate and datasource on the tableview object (probably a type, you are setting them on the view controller). It should look like this:
- (void)viewDidLoad:(BOOL)animated {
[super viewDidLoad:animated];
[self.tableView setDelegate:self];
[self.tableView setDataSource:self]; // <- This will trigger the tableview to (re)load it's data
}
Next thing is to implement the UITableViewDataSource methods correctly. UITableViewCell *cellOne =[[UITableViewCell alloc] init];
is not returning a valid cell object. You should use at least initWithStyle:
. And take a look how to use dequeueReusableCellWithIdentifier:forIndexPath:
. A typical implementation would look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// Reuse/create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Update cell contents
cell.textLabel.text = @"Your text here";
cell.detailTextLabel.text=@"text did appear";
return cell;
}
I can't believe I've been doing XCode programming for two years, and still hit this issue.
I had the same problem with XCode 6.1 - I was setting my UITableView
's delegate & dataSource in the viewWillAppear
function, but none of the delegate functions were kicking in.
However, if I right-clicked on the UITableView
on the Storyboard, the circles for delegate and dataSource were empty.
The solution, then, is to hold down the CTRL key, and drag from each of these circles up to the name of your UIView
which contains your UITableView
:
After doing this, my UITableView
happily populated itself.
(So, we're upto v6.1 of XCode now are we ? Do you think Apple ever going to make this thing, you know, friendly ...? I would quite like to add a Bookmark in my code... that'd be a nice feature.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.