How to change the class of UITableView programmatically? - ios

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

Related

UICollectionView custom class not work

I'm new in iOS programming and I apologize for the lack of experience.
I have a storyboard with UITabViewController and 2 tabs ( call it First and Second as example ). On "First" tab ( custom class - CFirstTabController ) I placed Collection View with custom class - CCollectionViewController. In it I placed Collection View Cell, added custom class to it - all as need.
Here class CCollectionViewController:
#import "CCollectionViewController.h"
#import "CSomeClassAsDataSource.h"
#implementation CCollectionViewController
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setDataSource:[CSomeClassAsDataSource init]];
}
return self;
}
- (void)reloadData
{
[super reloadData];
}
#end
And here is a problem: I want use other class as dataSource (CSomeClassAsDataSource implements protocol for it ), but I can't set it! Methods of this class not invoked. And I do not know why. I'm registered class CCollectionViewController as "Custom class" for my Controller View, but seems Xcode ignores it.
Your dataSource initialisation seems to be wrong as you've missed the alloc. Should be:
self.dataSource = [CSomeClassAsDataSource alloc] init];

UIScrollView delegate not getting fire?

I have subclassed UIScrollView and setting it's super class as delegate as shown in below snippet, even i have set contentSize:, delegate methods are not getting called.
popView = [[PopView alloc]initWithFrame:CGRectMake(35, y, 250, 40)];
popView.answerDelegate = self;
popView.delegate = self;
popView.contentSize = CGSizeMake(750, 40);
popView.scrollEnabled = YES;
[self addSubview:popView];
One more thing, 'PopView' is subclass of UIScrollView.
if you are subclassing UIScrollView then setting its delegate to self will not do anything. a scrollview cannot be its own delegate since it doesnt implement its delegate methods itself. the view that contains the scrollview needs to be the delegate for it and must implement its delegate functions for it to work properly.
also self != super class. super == super class
If you have to adding your Scrollview using Xib then check are set delegate to file owner.and Adding .h file Like this
#interface View : UIView <UIScrollViewDelegate>
if not work yet then trying to clean your build. and then try...

UITableView not calling cellForRowAtIndexPath

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.

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.

How to access subviews in View's initWithCoder method

I have a custom view which contains two UILabel. I want to customize their fonts before so I did that in initWithCoder method.
#implementation HomeTitleView
#synthesize ticketLabel;
#synthesize monthLabel;
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[monthLabel setFont:[UIFactory getFontForKey:#"home_month"]];
[ticketLabel setFont:[UIFactory getFontForKey:#"home_ticket"]];
}
return self;
}
#end
Unluckily, this did not work. Using a debugger, I found that monthLabel and ticketLabel are both nil. Anyone has idea how can I solve this? What callback or method I should implement so that I can access both of my labels?
You can't do that. The views don't exist yet. They are instantiated when the loadView method is called, which happens automatically when the view property is first accessed. If you want to manipulate your views after they have loaded, the correct method to use is viewDidLoad.
Edit: That's assuming you are working with a UIViewController class. If you are working with a UIView class, you can use awakeFromNib or didAddSubview:.
Do you ever assign monthLabel and ticketLabel to UILabel? Something like:
self.monthLabel = [[[UILabel alloc] init] autorelease];
self.ticketLabel = [[[UILabel alloc] init] autorelease];
If so, can you update your post with the code?

Resources