UITableView not calling cellForRowAtIndexPath - ios

I am trying to create a view controller with a map as the header (working), but my tableview will not populate and the cellForRowAtIndexPath method is never getting called. Any suggestions?
Code is on a gist here: https://gist.github.com/ohwutup/5229232
Here are some screenshots of my IB settings:

In your viewDidLoad method, add this.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Nearby";
self.tableView.tableHeaderView = _mapView;
self.tableView.delegate = self;
self.tableView.dataSource = self;
...
}

needed to do [self reloadData] in the completion block of my json request.

Your tableview's datasource and delegate should be your view controller, DOViewController. Then you tableview will populate correctly. Change the outlets in IB to point to your view controller class.

Related

awakeFromNib is called, viewDidLoad is not called

I have a view controller which get called from several places, and the first time it is called, awakeFromNib is called, but viewDidLoad is not called. viewWillAppear and viewDidAppear are called each time.
Also, the view (a UITableView subclass) functions correctly, except that anything within viewDidLoad is obviously not implemented.
loadView is not overridden in this view controller.
the code used to override viewDidLoad and awakeFromNib:
- (void)awakeFromNib
{
// Configure for self sizing cells:
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.allowsSelectionDuringEditing = YES;
self.clearsSelectionOnViewWillAppear = NO;
self.navigationItem.title = nil;
self.navigationItem.rightBarButtonItem = nil;
printf("\n awake from nib \n \n ---------- \n");
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.tableView.allowsSelectionDuringEditing = YES;
}
how is it possible that awakeFromNib is called but viewDidLoad is not, and how can I fix it?
edit: I received an answer that helped fixed it, however I would like to know why it happened, because it may be related to a bug in my app.
thank you.
This was a bit of a stab in the dark in a comment but it turned out to resolve the issue.
Ensure to call [super awakeFromNib] in the overridden method.

where to init a UITablewView as a subview

Where should I initialize a UITablewView as a subview, in - (id)initWithFrame:(CGRect)frame, in viewdidload or loadView? Which is the better approach? Where should I make the frame (I mean which is more effective)?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = kViewBackgroundColor;
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.backgroundView = nil;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.bounces = NO;
[self addSubview:_tableView];
}
return self;
}
On my projects I usually create a baseViewController with a custom initialiser like this:
- (id)init{
self = [self initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]];
if (self) {
//You should create the tableView and other properties here
//and add as subviews inside viewDidLoad
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,100,100)];
}
return self;
}
and I always create my view controller by using this init method, because I don't think the other vc's need to know the name of the nib file. If I were you I would create subviews inside init method, add as subviews inside viewDidLoad or viewWillAppear and finally releasing them inside dealloc if you are not using ARC.
Generally this depends on your requirement that if you are having custom view called with a tableview as subview then loadview with the initwithframe method will be better and while you want to initialize it from viewcontroller then viewdidload is better. I think you should bifurcate your requirement. Hope this helps.
Since you are initializing table view with zero frame it wont be visible even if you added that
you can try this
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = kViewBackgroundColor;
UITableView *_tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
_tableView.backgroundView = nil;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.bounces = NO;
[_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]
[self addSubview:_tableView];
}
return self;
}
If you'd like to create subviews programmatically based on superview metrics (frame or bounds), you should do it in viewDidLoad, because if you do in in initializer, metrics of self.view will not be avaliable, and you subviews will be created with zero frame and will not be visible (as Johnykutty) mentioned above.
But that does not seem to be a good practice in case your subview initialization routines require heavy operations.
Offtopic: I've been codin' for a looong time trying to do everything programmatically, never using xibs, etc. Well, if you ask me now, xibs really save your time and make life easier. Consider using interface builder for layout\autolayout, colors\borders etc stuff.

How to change the class of UITableView programmatically?

I have a UITableViewController and don't want to use a storyboard.
As far as I know, the UITableViewController takes care of initialising the UITableView, connecting it to its dataSource and delegate. This works very well in my case.
I would now like to change the class of the UITableView to a custom class (BVReorderTableView). This would be easily done in IB. However, once I do this programmatically, my UITableView is empty, that is it seems to be disconnected from its source and delegate.
Here is what I do in my init of the UIViewController:
-(id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization
self.tableView = [[BVReorderTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
What am I doing wrong?
You should implement this in viewDidLoad instead of init, because of the ViewController lifecycle. At viewDidLoad he already has all of the objects that are going to get used, which isn't necessarily true during init.
You can check this question for more information on ViewController life cycles.
:)
Please set your UITableView in viewDidLoad method not in initmethod. In init method view will initialize itself before subviews contained within it.
Hope this helps

Should we add UITableView in initializer or layoutsubviews method in iOS?

I am designing a Custom UIView for my app.
The UIView will comprise of below components:
UISearchbar
UITableView
My initialiser is below:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectZero];
_tableView = [[UITableView alloc]initWithFrame:CGRectZero];
_tableView.dataSource = self;
[super addSubView:_searchBar];
[super addSubView:_tableView];
// Initialization code
}
return self;
}
I am planning to set the frame of the _searchBar and _tableView in layoutsubviews method.
But I think think the way I have added the _tableView to super is wrong. Because the moment the _tableView is added to subview the data source methods of the _tableView will be triggered. And this happens even before the creation of the custom class itself.
Is this a correct design?
Can I add just _tableView alone in layoutSubviews as in below manner?
-(void)layoutSubViews{
//Adjust frame
[_tableView removeFromSuperView];
[self addSubView:_tableView];
}
You shouldn't be assigning the UITableViewDataSource in the view. It should be assigned in the ViewController.
You're right. There is no restriction on it. But your question is about design. Imagine something like this:
#implementation CustomViewController
- (void)loadView {
customView = [[CustomView alloc] initWithFrame:CGRectZero];
customView.tableView.dataSource = self;
customView.tableView.delegate = self;
}
With a ViewController, you can control when you initialize your custom view and control when its tableView loads the data. While you can certainly put all of this code into your customView, you will be running into problems much worse than the one you are asking about now.
You should definitely add it in init, because layout sub-views will get called each time you view will resize and will need to re-layout its sub-views.
Layout subviews method is strictly use as a callback telling you that your view will layout, and is used as an override point for any additional layout you wish to make.
Also, as an additional note, it's not good design adding the view using super.

iOS: Dynamic Creation of UISegmentedControl within UIView

I am wanting to create a custom UIView class that will show a dynamic number of UISegmentedControl objects depending on some input. For example, if a client has 5 products in their cart, the UIView should generate 5 UISegmentedControl objects that I will then link with each item.
The problem I am having is getting this to work in a UIView. Here is what I have done so far. I am successfully able to create a UISegmentedControl object and display it programmatically within my main UIViewController. I don't get any display when adding it to my UIView class. Here is the implementation code for the UIView class:
#import "ajdSegmentView.h"
#implementation ajdSegmentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *itemArray = [NSArray arrayWithObjects:#"Yes", #"No", nil];
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:itemArray];
button.frame = CGRectMake(35,44, 120,44);
button.segmentedControlStyle = UISegmentedControlStylePlain;
button.selectedSegmentIndex = 1;
[self addSubview:button];
}
return self;
}
#end
I created a new UIView object via Storyboard and placed it inside the UIViewController scene. I made sure to set the class from the generic UIView class to my new custom class. I added and outlet for the UIView in my UIViewController class. Here is the code inside the implementation of UIViewController:
#import "ajdViewController.h"
#interface ajdViewController ()
#end
#implementation ajdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.segmentView = [[ajdSegmentView alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
That's all I have tried. I have been searching through a lot of pages and trying to implement this without asking here, but I seem to be looking in the wrong places.
First you need to check ajdSegmentView is UIVIew or UIViewController. It is fine if it is UIView. If it is type of UIViewController then you need to add this line while adding Segment.
[self.view addSubview:button];
In place of:
[self addSubview:button];
And One more thing You forget to add this View to your main after allocating so You can declare like this:
objajdSegmentView = [[ajdSegmentView alloc] init];
[self.view addSubview:objajdSegmentView.view];
I have just added this thing. i got result like this way.
Hope this will work for you.
You're initializing your custom view using the init method, but your initialization for ajdSegmentView is in your initWithFrame: method (which in your case is not getting called).
So replace:
self.segmentView = [[ajdSegmentView alloc] init];
with:
// Change the frame to what you want
self.segmentView = [[ajdSegmentView alloc] initWithFrame:CGRectMake(0,0,100,40)];
Also don't forget to add your view to the view controller's view also.
[self.view addSubview:self.segmentView];
Unless this view is being created with interface builder, in which case you will need to override initWithCoder: in your ajdSegmentView class.
I'm not familiar with Storyboard though, so maybe I'm missing something, but in a standard scenario what I said above will solve your problem.

Resources