UITableView with UIDatePicker? - iphone

please I need some advice about design approaches in iPhone programming.
I want to display a table view (with three cells that will never change) and a UIDatePicker.
I don't use Interface Builder.
I created a UIViewController subclass and tried to put all together in its viewDidLoad method:
UITableView *myView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 250) style:UITableViewStyleGrouped];
[self.view addSubview:myView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 320, 216)];`
and other initialization params...
[self.view addSubview:datePicker];
I miss something, I don't understand how to add UITableViewCell objects to myView?
I'm all wrong?

You add it in the tableView:cellForRowAtIndexPath method in the UITableViewDataSource protocol. See the example here: http://iosdevelopertips.com/user-interface/creating-unique-looking-tables-with-custom-cells.html

Yes, as jeremie stated you need to add the some UITableViewDelegate and UITableViewDataSource methods to get a UITableView fully functional.

Related

Programmatically created UITextField hides other controls

I've requirement to create dynamically some controllers. In the image provided here I've programmatically added an UITextField (name), which hides UITableView.
UITableView is hidden by default. When user touches the UIButton above it, UITableVIew gets appear.
My question when UITableView gets appear, how can I make UITableView top of all other controls?
you will have to change the sequence.
Add UItextfield first
[self.view addSubview:yourTextField];
and add tableview and other views after that line of code so that they appear above it.
[self.view addSubview:yourTableView];
Try
[self.view bringSubviewToFront: yourTableView];
I think this will work fine so please implement this one.
UITextField *txtFld = [[UITextField alloc] initWithFrame:CGRectMake(65, 300, 200, 30)];
txtFld.backgroundColor = [UIColor clearColor];
// Border Style None
[txtFld setBorderStyle:UITextBorderStyleNone];
[txtFld setPlaceholder:#"Name"];
[self.view addSubview:txtFld];

how can i add a view in tableView using interface builder

hi i'm studying iOS programming.
i have a wondering using interface builder.
i make a tableViewController and also make .xib file
now i can see the UITableView in interface builder.
i have to add a view, called myView, that contains buttons, labels and so on.
i want to myView be set the top of tableView's area, like tableview's header.
so i make a myView, and add buttons, labels, etc.
and i drag that view into UITableView. ok here's fine.
i can see myView is set the top of UITableView in interface builder.
but i run the program, myView doesn't appear.
of course wire up using IBOutlet, and declare property and synthesize.
but i use NSLog like this
if(self.myView == nil)
NSLog(#"omg it's nil!!!");
i can't understand that NSLog is printed my prompt area.
because i make that view in interface builder!
i can see tableView, of course can see the cells.
why myView doesn't appear??
how can i fix it??
i want to know how can i fix it using interface builder.
please help me
Not sure if this is possible using interface builder, I usually create a view manually and add it to the tableview header in the viewWillAppear method like so:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10,self.tableView.bounds.size.width, 50)];
label.text = [person getFullName];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:25];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
[headerView addSubview:label];
self.tableView.tableHeaderView = headerView;
A simpler method would be to create a view in a separate nib file and then load it into the table header when you load the tableview like this:
UIViewController *aViewController = [[UIViewController alloc] initWithNibName:#"MYXIBFILEHERE" bundle:nil];
self.tableView.tableHeaderView = aViewController.view;
My experience with XCode 4.3:
you have to first place UITableView and UIView on the same super UIView. Then, when dragging UIView to the top of UITableView you'll notice XCode highlighting the option of inserting your view as footer.
Once done, you can detach UITableView from superview and delete it.
The only way I've been able to get this to work is as follows:
Add new nib file to project. Lay out the view in the nib file as you
want your table footer/header to look.
Set the class of the File's Owner in the nib to the class of your
UITableViewController. This is so we can connect the view in this
nib to an IBOutlet in the UITableViewController class.
Set a UIView IBOutlet in your UITableViewController to point to the
view in the new nib.
In your UITableViewController class, do something like this in
viewDidLoad:
(apparently the code won't format correctly if it's directly after a numbered list.)
[[NSBundle mainBundle] loadNibNamed:#"statsFooterView" owner:self options:nil];
self.tableView.tableFooterView = myFooterView;

Show some help text over a UITableView if the UITableView happens to be blank

I've got an app with several UITableViews in it, and if one of them is empty, I'd like to display a bit of help text, floating in front of the UITableView:
"Looks like you haven't created anything for this list yet. Make your first entry by tapping the button above."
How do I present this view, floating in front of the UITableView?
What method of the UITableViewDelegate would be used to present this view?
What would the code look like to present the view?
What method of the UITableViewDelegate would be used to hide this view once the user adds content?
Here's a quick mockup:
declare in .h file
UIView *root;
in .m file
import QuartzCore/QuartzCore.h
- (void)viewDidLoad
{
[super viewDidLoad];
root = [[UIView alloc] initWithFrame:CGRectMake(60, 50, 220, 250)];
root.backgroundColor=[UIColor grayColor];
root.layer.cornerRadius=10;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
label.backgroundColor=[UIColor clearColor];
label.numberOfLines=3;
label.font=[UIFont fontWithName:#"Arial" size:14];
label.lineBreakMode=UILineBreakModeTailTruncation;
label.textColor=[UIColor whiteColor];
label.text=#"Make your first entry by tapping the button at the top of the screen";
[root addSubview:label];
[self.view addSubview:root];
}
inside your button Event method
[root removeFromSuperview];
in viewDidLoad/Appear,
create a view and add it as subview [self.view addSubview:infoView] . on click event of + button remove it from superview [infoView removeFromSuperView]
I would create/remove the view in numberOfRowsInSection (if it is 0 then show the view) or whatever it is called. As for the view itself, have a UILabel in a custom view. To add the rounded corners etc you can access the view's layer, which is a CALayer. Oh and make sure that you have an ivar for the view so you can remove it easily and check if it is visible. And make sure you always reload the table view when you show your view, as otherwise numberOfRowsInSection will not be called.

Design UI programmatically

I'm trying desperately to design my UI programmatically.
But I think I'm in trouble to understand the concept.
Basically I want a UI which contains different UIViews and a UITableView.
But now I'm stuck with the UITableView.
So my "main view" which should contain all these views is called AddListViewController(it inherence from UIViewController).
In the loadView method of this class I'm trying to add a table, but no chance. Has anyone a good example for me. I'm really dont see the point for a separate UITableView and UITableViewController.
You can create a new UITableView, very simply like this:
UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
[tableView setDataSource:self];
[tableView setDelegate:self];
[self.view addSubview:tableView];
[tableView release];
EDIT: The above answer beat me to it.
Just create your UITableView and add it as subview:
UITableView *table =
[[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height)
style:UITableViewStyleGrouped];
...
[self.view addSubview: table];
[table release];
In loadView, you have to make every view - including the overall view associated with the view controller.
You perhaps want to do what you are trying to do in viewDidLoad instead?
i many be late in answering but still i am answering it
use:
shareListView.delegate = self;
shareListView.dataSource = self;

Non scrolling tableViewHeader

First a little background info:
I have UIViewController that contains a UITableView. In the loadView method (after initialization of the table), I set the UIViewControllers view to the table view with: self.view = tableView;
What I want is a view on the top of the screen (before the UITableView), that doesn't scroll with the rest of the table view when it is scrolled. I have tried adding my UIView to the table view's tableViewHeader, which displays correctly but scrolls with the rest of the table.
Is there any easy fix for this? Either way, any hints towards a solution is greatly appreciated.
EDIT:
Come to think of it, what I want is something like the stock application where the bottom part is stationary and the rest of the screen is a UITableView. The only difference is that I want the stationary part at the top of the screen.
As kmit has already pointed out, you can easily add more than one subview to your view. So, don't set the table view directly as self.view, but rather create a blank UIView (as container) and add the table view as well as the header view as subviews to that view. You can control the views' extents via their frame attributes. A simple example:
- (void)loadView {
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
// header view
HeaderView* headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 182)];
self.headerView = headerView; // in case you need the reference later on
[view addSubview:headerView];
[headerView release];
// table view
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 182, 320, 186) style:UITableViewStylePlain];
[tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
tableView.delegate = self;
tableView.dataSource = self;
[view addSubview:tableView];
self.tableView = tableView;
[tableView release];
self.view = view;
[view release];
}
As an alternative to creating the containing UIView manually, you can call [super loadView] at the beginning of your loadView implementation.
Is there a reason you are setting the view of the UIViewController to that of the UITableView? Why not handle the UITableView as a subview? That would allow you to add anything you want above the UITableView -another view, empty space with the view of the UIViewController as your background, etc.