Hello I want to have a table of UILabel. The first column is the name of an item and the second is the value of that item. The problem is I don't know how many rows I need until the user logins in. I know how to add rows to a table in Android however it seems impossible to do with the iPhone. How would I achieve this?
Consider using a UITableView with custom cells.
The advantage is that, depending on your data source, a table view can automatically update when your data changes. An event-driven architecture (ie: update table when data is available/changes) is in-line with objective-c land.
For examples of custom cells: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
For tutorials on making a UITableView backed by a data source, I'd point you to the excellent Stanford iOS programming course, available on iTunes U.
There is a view called UITableView special for these aims. Read this article:
Table View Programming Guide for iOS
UITableView Class Reference
You need to implement the UITableViewDataSource protocol. In this case, one method of the protocol is this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
You can see the entire protocol documentation here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
You can do it in two different ways.
In the first one you can create labels programatically. A label (UILabel class) inherits from UIView. So, if you have a superview, you can add labels on it depending on user login. You have to position labels using the frame property. For example:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 40, 300, 50)];
titlelabel.text = #"title";
[ySursuperview addSubview:titleLabel];
[titleLabel release];
// add labels here
In the second case you could use a UITableView (that contains an header view for the title) and a where each UITableViewCell contains NameX and ValueX. You could use also a custom UITableViewCell.
In my opinion the second solution could be more elegant and easier to maintain. The first one, instead, is more quickly but you have to do some calculations to obtain the grid layout you want.
Related
I want to add a tableview-look-a-like-login to my app, but it seems to be not that easy to implement. I tried to accomplish my goal using more then one approach, but i am not sure about which solution is the best.
For example, Dropbox and Facebook have a login page like this.
Here are my 3 approaches :
I added 2 UITextfields to my View (no border) and placed a . png behind, which looks like a tableviewcell. ( Not the best approach cause i want to use real tableviews )
I added a Container View to my ViewController placed a tableview with static Table Views inside. The Problem here is, that i dont know how to access the information inside my viewcontroller?
I added a tableview to my ViewController and used dynamic cells with it. Connected the outlets for delegate and datasource to my viewcontroller and initialized them with the delegate and datasource methods. The Problem here is, that i can not use static table views inside a uiviewcontroller.
Is there any better way of solving this problem ?
I would really like to know how to do this in a more elegant way.
EDIT:
A ContainerViewController basically solved this issue for me some month ago.
After embedding one into the main controller you can access it through the prepareForSegue function and define a protocol-based interface for that specific controller to interact with the embedded controller.
If you want to use static cells inside a regular UIViewController, just add the static cells and design them the way you like in interface builder, then connect the table cells as strong IB outlets (weak won't work, make sure they are strongly referenced). This will work flawlessly if you have a few table cells. Then set the view controller as the data source of the tablet view, implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of cells and implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return your strongly referenced cell instance for the specified index path. I've used this method for a simple table view in my view controller that had four cells and it is working perfectly. For a large-dynamic data set, I definitely do not recommend this approach but for small, static tables, this does the job right.
I have an idea how to solve this. I think it's a clean way to do so. You do not need storyboard for this controller.
Make your controller subclass UITableViewController like so:
#interface YourViewController : UITableViewController
Then in your viewDidLoad you create the instances of the cells:
- (void) viewDidLoad {
usernameCell = [YourTextFieldCell new];
passwordCell = [YourTextFieldCell new];
}
The YourTextFieldCell is of course your own subclass of a UITableViewCell, which could be something like this:
#implementation YourTextFieldCell {
UITextField textField;
}
- (id) init {
self = [super init];
if (self) {
// Adjust the text's frame field to your liking
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
[self addSubview:textField];
}
}
// A getter method to access the textfield from the outside
- (UITextField *) textField {
return textField;
}
#end
Back in YourViewController:
- (NSInteger) tableView:(UITableView *) tv numberOfRowsInSection:(NSInteger) section {
return 2;
}
- (UITableViewCell *) tableView:(UITableView *) tv cellForRowAtIndexPath:(NSIndexPath *) indexPath {
if (indexPath.row == 0) {
return usernameCell;
} else if (indexPath.row == 1) {
return passwordCell;
}
return nil;
}
Do you get where I am going with this? This is how I think you should do it! Good luck!
I think your approach 2 is the best. If you need to access information in the table view controller, from your UIViewController (which will be the parent view controller), you can get a reference to that table view controller with self.childViewControllers.lastObject. In the viewDidLoad method of the UIViewController subclass, you could set yourself as the delegate of the table view with this line if you want:
[[(UITableViewController *)self.childViewControllers.lastObject tableView] setDelegate:self];
That way, you could implement the tableView:didSelectRowAtIndexPath: method in the view controller, which will get the information I'm guessing you need.
If you go with your option 2) using a storyboard and have a ContainerView containing your own subclass of UITableViewController with static cells then you can implement the prepareForSegue: method in your parent ViewController to take a reference to the UITableViewController (it'll be the destinationController of the segue) and also to pass itself down to the UITableViewController subclass if necessary (which should hold onto it with a weak reference).
Disclaimer - This answer will work for any size of UITableView, but if you're just making a login view, Tom's answer will work quite well.
I'm not sure if this will help, but what I did for this was create my own UITableView-esque subclass with a UITableViewCell-esque subclass as well.
This may not be what you want to hear, but I find what I made to be really helpful, since I've used it a number of times now. Basically, you have a UIView with the stylistic approach for the different types (10.0f - 20.0f cornerRadius and a 1px border (divide by UIScreen's scale property for retina). As for the cell, you'll want to have a full sized UIButton on it that responds to your table view for the touch events either with a delegate or by setting the target and tag inside your table view's class.
Last, you'll have a delegate system just like the UITableView for your information for building the specific tables.
In short, you'll need:
2 UIView subclasses (TableView and TableViewCell)
2 Delegates/Protocols (TableViewDataSource and TableViewDelegate)
Optionally
1 Delegate (TableViewCellResponseDelegate)
1 NSObject Subclass (Contains all of the information needed in each cell - Ease of use)
I found Can's solution to be the best / easiest, but unfortunately it breaks in XCode 5.1 --
I found a workaround which builds off the same basic idea, but unfortunately requires a little more involvement: http://www.codebestowed.com/ios-static-tableview-in-uiviewcontroller/
To summarize, you can add TableViewCells directly to views (and create IBOutlets from them, etc), but in order for them to get "moved" to the TableView properly, you need to remove them from the view in code, and you also need to set Auto-Layout constraints in IB.
I am a very new developer for IOS, i need help, How to create dropdown box in xcode, any one provide me example to create country list in drop down?
Here I found two demos for dropDown list, One is creating custom expandable UITableViewCell like :-
to
source code :- DEMO
AND Another is custom Drop Down list like:-
by clicking of test then open drop down list as bellow like image
source code with Tab Bar:-DEMO
updated source code without Tab Bar :-
http://www.sendspace.com/file/83qws5
I beleive you shouldn't use dropdown boxes in iOS, as it's a desktop OS UI control element. You should think up something else using existing components (like PickerView), that's the words for UI consistency.
And if you need this anyway, you may create a table view, place it beneath your label and a triangular button(which causes it to appear and disappear) and populate it with values.
Since there are no native DropDown elements in iOS, you could make use of a TextField with custom background and a UITableView to accomplish that. Here is how to go about it.
Pseudocode
Create a TextField and set it's delegate to parent controller
Implement UITextFieldDelegate and implement the textFieldShouldBeginEditing method
Create a new UIViewController and implement UITableView in it programmatically.
Create a custom protocol and create it's object (delegate) it.
In textFieldShouldBeginEditing method, load this controller and present it modally passing the required table's data source and set delegate as parent.
in the new tableViewController, implement UITableViewDelegate and implement the didSelectRowAtIndex path method.
Upon row selection, call the delegate with passing appropriate data.
dismiss the modally presented controller.
just for whom searching for small simple swift combo box here in 2016 year, i've tried a few of old and new (but obj-c) libs, and at last selected this:
https://github.com/sw0906/SWCombox
here is screenshot:
The easy and simple way to design a drop down list is by representing it like a UITableView and some animation. This makes it look really like a dropdownlist. Here is a code I used for creating one . For this first import the < QuartzCore/QuartzCore.h > framework.
-(IBAction)DropDownTable:(id)sender
{
TableView.hidden = NO;
if(TableView.frame.origin.y ==203)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
[TableView setFrame:CGRectMake(224, 204, 27, 160)];
[UIView commitAnimations];
[self.view TableView];
}
else if (TableView.frame.origin.y == 204)
{
[TableView setFrame:CGRectMake(224, 203, 27, 0)];
TableView.hidden = YES;
}
[self.view addSubview:TableActivityLevel];
}
First make a tableview , declare its methods and make the array . Put this function on the click of a UIButton and youll see it work !!! Happy coding :)
I have a plain UITableView in my project with sectioned data. For each section I have a header for every letter so it looks like the contacts app on the iPhone. The only thing I want to change, however, is the color of the header background. This code doesn't work but this is what I want to achive:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UITableView alloc] init] tableHeaderView];
[headerView setBackgroundcolor:[UIColor redColor]];
return headerView;
}
I could design my headerView from scratch by initing a UIView and building it to look like the header does now, but then I need to write much more code and have to guess the properties since I don't know what the code for a regular header is and since I just want to change the background color I thought there might be an easier way.
Thanks!
I just wanted to add an answer to this question myself if anyone else come across this issue and want to know what to do. The short answer is that you can't simple just change the color of the header, but, you can write some more code to mimic the gradient and shadows to make it look like the headerView. The exact code for doing this can be found in this other question: How can I access the standard viewForHeaderInSection for a tableView?
I'm looking into creating a simple iPhone app without using the interface builder, i.e. without creating the XIB files.
I've succeed so far in showing the main window and changing the background color, but i'm looking into adding UITextfield, UILabel, and a button. and then connect them to methods that i've created before.
is there any good tutorial or a reference that I can use?
Thank you very much
Read Apple's documentation. For example, to create a UILabel programmatically, read the UILabel Class Reference. First instantiate it
UILabel *label = [[UILabel alloc] init];
Then set it up as you like, e.g.
[label setText:#"My label now has text!"];
Then add it to the view
[self.view addSubview:label];
Then move it around, etc. For more help, first google "create uilabel programmatically", then, if you get stuck, post a specific question here.
I'm still very new to cocoa touch so please excuse any terminology that I may have got wrong.
I have a bunch of images in my bundle consecutively named image0.png, image1.png etc...
I have a picker in a viewcontroller and an instance variable that keeps track of the current row.
When a user clicks a button I want to be able to create a uiImageView object (and first test whether the view already exists) based on the row number, ie: uiImageView *imageView1 for row1.
Is this possible or would it just be easier to create a line of code for each possible case?
I hope this makes sense, and thanks in advance for anyone that can shed any light!
Why not just create one UIImageView in Interface Builder and then swap out the image it's displaying depending on your picker index?
Implement:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
[imageView setImage:[imagesArray objectAtIndex:row]];
}
Where imageView is an instance variable (IBOutlet) connected to the image view control in Interface Builder.
Ok, that makes sense.
I think what I was trying to do was take lazy loading to the extreme by only creating the uiImage object and the uiImageView object the first time it was called on from the picker (having clicked the "go" button).
I anticipate having around 40 images that will fill the screen.
Programatically, would this be an acceptable method for this number of images?
Thanks very for the responses guys.