How to resize a UITableView - iphone

In my application, I have a button that pushes a custom view controller (MenuViewController) on click.
#interface MenuViewController : UITableViewController
I wasn't able to resize the tableview. I tried:
self.tableView.frame=CGRectMake(0, 0,100, 100);
self.View.frame=CGRectMake(0, 0,100, 100);
in viewDidLoad and viewWillAppear. Neither of these methods worked.
I have an idea of creating a UIView initwithframe:.. and add MenuViewController as a subview.
Is this the proper way to do it?
MenuViewController is basically a general control that will handle variable init inputs
and will display the cells according to the inputs, the problem is the frame of the table view.

I believe that if you inherit from UIViewController and expose the UITableView as a property and also on the Interface then you can resize it. When you are using UITableViewController then it will always display in full size mode.
UPDATE:
So instead of doing something like this:
#interface TasksViewController : UITableViewController
{
}
You can do something like this:
#interface TasksViewController : UIViewController<UITableViewDelegate>
{
}
#property (nonatomic,weak) IBOutlet UITableView *tableView;
Also make sure that in interface builder you have a UITableView which is hooked to the tableView property in the TasksViewController. Now in your implementation file implement the cellForRowAtIndexPath method and return the cell.

Related

Assign own View Classes to Storyboard

See this storyboard containing a Navigation View, Gestures, and a few Views.
Also within is a TabView, which contains an UIImageView and UITableViewController.
To apply my data to this TableView, I need to assign a Controller by using msExampleTableViewController.
The problem: the input/dropdown field removes entry after pressing Enter (like if this class is not assignable).
What is wrong here, and how can it best be done?
NA UITableViewController does assume that the root view will be a UITableView. When you combines UITableView with others controllers.. you can do in your Controller (ViewController in your case):
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, assign) IBOutlet UITableView* tableView;
In Interface Builder set both the delegate and dataSource outlets of the table view to be equal to its superview's view controller (File's Owner)...
Now, Your view controller implementation should be the typical TableViewController implementation: cellForRowAtIndexPath, etc.

Common views in viewControllers - Code re-usability

I have few common views in most of my viewControllers. What I noticed is that I can reuse single code for all viewControllers which is absolutely wise. For this I decided to create a class Utils which has static methods like
+(void)createCommonViews:(float)yAxis:(NSString*)text;
In my case common views are three labels and two images.
Problem : I am not able to add these views from Utils. I am wondering how can I send self as a parameter so that I may add the views from Utils. It may be wrong to add views outside the viewController. In that case what can be the solution? Taking all these views in a UIView, setting return type of Utils method as UIView and then adding UIView to viewController (after calling method from viewController) might solve my problem. But what I am looking for is some other solution.
+(void) createCommonViews:(float)yAxis withText:(NSString*) text toTarget:(UIViewController*) target
{
//create views
[target addSuview:view];
}
But I think returning a Uiview and then adding it in the UIViewController afterwards, is a far better solution.
The method you're attempting is to have your view object as a singleton. This is uncommon at best, at worst a crash waiting to happen. Better design is for each of your view controller classes to have its own instance of your custom view, like so:
#interface MyCommonView : UIView
// ...
#end
#interface MyViewController_A : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
// Meanwhile somewhere else...
#interface MyViewController_B : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
Create a viewController that acts as a parent view for all your common stuff, call it CommonViewController then implement this in all the viewcontrollers you want it to appear
-(void) viewDidLoad
{
[self.view addSubView:[[CommonViewController alloc] initWithRect:..];
}
Or alternatively using xib files

didSelectRowAtIndexPath not get called

I have a UITableView inside a UIViewController like so:
.h
#interface OutageListViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *outageTable;
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected");
}
I have customized table cell:
//EDIT
I have fire UILabel side by side on my customized view, as well as a background spreading the entire area. But after resizing/removing the background image and label I putted on the customized cell, "didSelectRowAtIndexPath" is still not being called.
//END EDIT
#interface AbstractSummaryListViewCell : UITableViewCell {...}
and
#interface FiveColumnSummaryCell : AbstractSummaryListViewCell {...}
This UIView is inside another UIView:
#interface CustomTabBarController : UIViewController {
OutageListViewController *outageListViewController;
And my AppDelegate add this to the window:
[window addSubview:[customTabBarController view]];
Now I'm trying to determine which cell get clicked and didSelectRowAtIndexPath doesn't get called, I have dataSource and delegate connect from the UITableView to File's Owner, in fact the data populates correctly as my "cellForRowAtIndexPath" specifies, any ideas how can I fix this?
Thanks!
I solved it: forgot to check User Interaction Enabled in my customized cell xib. What a fool!
Are the following properties of UITableView all YES?
allowsSelection
allowsSelectionDuringEditing
Edit:
I think Paul is right. The delegate property has some problem. You can check the delegate property of tableView inside -(void)viewDidLoad. As you said, they should be connected to FileOwner in xib. So the following codes won't obtain nil.
- (void)viewDidLoad {
[super viewDidLoad];
// They should not be nil.
NSLog(#"delegate:%# dataSource:%#", self.tableView.delegate, self.tableView.dataSource);
}
It's possible that the view controller has not been connected to the delegate property of the outageTable anywhere.
You can make a quick test... Remove the "big" label and see if the didSelectRowAtIndexPath is called.

How to make a .xib show up in a custom class

I was wondering how to connect my custom class and my .xib. In Interface Builder I have changed the class of the Files Owner to my custom class. I then created an IBOutlet and connected it to the view of the .xib. I then added an instance of my custom class to my UIViewController. My custom class is a subclass of UIView, so I set the background of it to black and can see it appear on top of my UIViewController. What I can't see is my .xib???? Here is my code for my custom class... (FreeThrow is my custom class... FetView is my .xib)
#interface FreeThrow : UIView {
IBOutlet UIView *mySquareView;
}
#property(nonatomic, retain)IBOutlet UIView *mySquareView;
-(void)createMe;
#end
#implementation FreeThrow
#synthesize mySquareView;
-(void)createMe {
[self addSubview:mySquareView];
[self setBackgroundColor:[UIColor blackColor]]; // I did this to know my UIViewController is showing this class... which it is
}
#end
Here is my code for what I call from my UIViewController...
freeThrowView = [[FreeThrow alloc] init];
[freeThrowView createMe];
freeThrowView.frame = CGRectMake(8, 42, 335, 230);
[self addSubview:freeThrowView];
What code do I need to add? What do I need to do? What is the problem? Why is my .xib not showing up.
Sadly apple doesn't give us a way to truly link a xib to a view via IB alone. You'll have to do it via instantiation. Here are some past answers to similar questions:
How do I associate a nib (.xib) file with a UIView?
Loading .xibs into a UIView

reloadData for a Table View in a View Controller

Hello I have a view controller that loads from an xib i created. It has two toolbars and a table view in that.
I add this too the header file in the ViewController
#interface FilterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
When I do
[self.tableView reloadData]
It does throws up an error and does not build.
Just making your UIViewController conform to UITableViewDataSource and UITableViewDelegate does not automatically give you a tableView reference.
You need to create a tableView IBOutlet and connect it in Interface Builder.
Also, why not just inherit UITableViewController instead of a UIViewController?