creating a Table View in Xcode 4 - iphone

I'm new to Xcode 4, and having some problems with TableView.
I have a ViewController which I dragged into a TableView.
I have the source model, and created an array,
but I don't know how to get the TableView to display the values.
I don't want to use a TableViewController, because I want the table to be smaller than the IPhone screen - but without it I don't know how to connect the TableView to a controller.
can someone provide me a good step-by-step explanation of how to do it?
thanks

A simple and efficient example of creating a TableView , here

declare your view controller as conforming to the UITableView delegate and datasource protocols
Connect the tableview's delegate and datasource outlets to your view controller in interface builder
implement the methods as described in the documentation.
Table view controllers give you a little extra in terms of editing mode and a few visual features on the table, but you can add a table view to a view managed by a standard view controller and be just fine.

If i understoof the question correctly to connect the smaller table to the code, you right click on the table in the interface builder and connect the delegate to the files owner it should work.
Otherwise if you check out http://www.geekylemon.com they have some really good tutorials
Cheers

Related

Reuse a custom UITableView in different UIViewControllers

I'm developing an iPhone app for iOS 5.1. I built a UIViewController which has a UITableView with a UISearchDisplayController and other views, all somehow related to the table. The UIVIewController is the delegate and the datasource of the table, and makes some customizations (graphical and business-related) to the table and search display controller. So far, everything was OK.
The problem is that now I want to put this same table (maybe with a different sublist of elements, of the same type) in different controllers (different screens of the app). I tried to add the tableView of my controller as a subview in other controllers, but it doesn't work. I tried to rebuild my table as a subclass of UIView (instead of UIViewController) and add it as a subview, but it neither worked (it loaded the view from a NIB file, but all its properties, including the IBOutlets, where nil or 0x000000).
After searching a lot, I didn't find any example of how to reuse a tableview in different controllers. Any hint? Any example? Should I build it as a UIViewController or as a UIView? Which class should be the delegate of the table and searchdisplay, keeping in mind that most of the logic I want to reuse is the code in the delegates?
Thank you all in advance
Wouldn't the easiest solution be to create your own Datasource class (maybe as a singleton) and then reuse this with the other controller? This way, your way of getting and managing the data is abstracted from the way of displaying it. Just the way it should be.
This is what MVC is all about.

reuse view from storyboard

I have a tableview with custom section headers. The view for the section header is defined in the storyboard and wired to an instance variable. Is there a way to request a new instance of the view from the storyboard?
In the past I have done this by having the section header defined in its own xib file and getting a new instance by using
[[NSBundle mainBundle] loadNibNamed:#"TimerViewSectionHeader" owner:self options:nil];
UIView *newHeaderView = self.sectionHeaderView;
I dont' think there is a way to do that. Best bet is to put the tableview custom header view in a separate nib and load it like you did in your code sample whenever you need to use it.
I tried to do the same thing and ran into the same problem.
I like to work with storyboards a lot and was impressed how fast I could create a working UI. However, as soon as you need to re-use views it makes a lot of sense to put those into a separate nib along with its UIViewController subclass.
You can then place a generic UIView in all the places where your re-used view should go and add the view using your ViewController:
[myReusableViewController loadView];
[myReusableViewController viewDidLoad]; // You have to handle view callbacks yourself.
[self.myReusableViewPlaceholder addSubview:myResusableViewController.view];
[myReusableViewController viewWillAppear:YES];
So to sum it up:
Use storyboard, it's great
Create the scaffold of your application in the storyboard, along with any static view (like About screens etc.)
Create re-used views in a custom nib + UIViewController subclass and add UIView placeholders in your storyboard.
In another answer I thought about some Pros and Cons of Storyboard
The solution I've come up with for this is as follows:
I have a tableview with multiple prototype cells that displays complex data. There is a segue to a detail view, and a transaction process view.
This first tableview has a search button that displays a new tableview with the results. It needs the same functionality as the main tableview that pushes it; including segues to the detail and transaction progress views so:
On storyboard, select and copy your main tableview. Deselect and paste. Create a push segue from your main tableview to your 2nd tableview; or from where ever you want to navigate to it from. Modify the 2nd tableview as you like. IE: If it requires some UI changes no problem.
Create a new viewcontroller class that is a subclass of the viewcontroller running the main tableview.
Override the data delegate in your subclass to serve up the subset of data you want.
Back in the storyboard, select your 2nd tableview controller and in the identity inspector select your subclass as the custom class.
For this solution to work smoothly, your app really needs to be managing data for the views. You could use prepareforsegue to pass data from 1st tableview to the second, but I've found the app data model far more flexible from numerous points of view.
Unless you have buttons that push to the sub views via segue, your subclass will need to override functions that push via segues with identities. NB Segues must have unique identifiers if you id them at all.
It took a lot of trial and error to figure this out, but once you understand the concept, it's a relatively smooth solution that is quite adaptable and not so bad to implement.
I am not sure about just views, but the way that I was able to get view controllers out of my storyboard is as follows.
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
From here, perhaps you might be able to use this similarly to how it was once done with nibs.
I've been able to reuse a view in the storyboard just by connecting a transition from one tableview into the one I want to reuse.
so my tableview that I want to reuse is pointed to twice.
It sort of works but the problem I'm running into it setting a variable (using instantiateViewControllerWithIdentifier) in my app delegate to my table view that is getting reused.
It seems that if I reuse it, the storyboard is creating 2 instances of my tableview and the one I get with instantiateViewControllerWithIdentifier isn't the one I want.
I'm not really sure if this is the proper way to do it. But I assume many others are doing this somehow. With the custom table cells in storyboard I suspect lots of people want to reuse their views.
For example: We want to reuse the view(include subviews) in storyboard shown below.
The best solution I know so far is clip and paste the view related code to the New Singe View file without losing the information.
Detailed steps are as follows
Step 1: Rename the view we want reuse. Just prepare for step 2.
Step 2: Open storyboard as source code in order to clip the XML code we need
Step 3、4: Search and clip the code we need
Step 4.5(Not needed): Open as Interface Builder to see the view removed
Step 5、6: New XXX.xib and paste the code we clipped just now
Step 7: Important. Insert code<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> to XXX.xib source code.
Warning: Do this before open it as Interface Builder! Otherwise, you will see wrong size and layout waring.
[![step 7][9]][9]
Step 8: New XXX.swift to connect the XXX.xib
[![step 8][10]][10]
Step 9: Add the view anywhere we want
[![step 9][11]][11]
I get warning: "You need at least 10 reputation to post more than 8 links."
Can you support me to upload the remaining 3 screenshots?

how to implement swipe in table view iphone

i have a table view. i divided it into 3 sections.
i want to implement swipe in this tableview. when i swipe in table view, next view will be loaded.
How to implement this?
Thanks in advance
You have basically two options:
I: Use UISwipeGestureRecognizer
Since I never worked used it, there is not much I can tell you about this way. Just see the official documentation for further information. You should know, that it was introduced with iOS 3.2, so there is no support for iPhones which are not running iOS 4.0, so especially firstGen iPhones will be excluded.
II: Overwrite touchesBegan/Moved/Ended
Read this post for further information, this should be exactly what you need. Of course, this solution does not only work for UITableViews but for every class that inherits from UIResponder (and consequently for every UIView).
Generally, your table view controller will implement the UITableViewDelegate protocol. The tableView:didSelectRowAtIndexPath: method is called when the user touches one of the rows in your table view. If you implement the method, you can use row and section properties of the NSIndexPath that are passed in to determine which row in you table the user selected. Based on the selection, you can then create or initialize the appropriate next view controller that you want to load and then push the new controller on the table view controller's navigationController.
For further info, try reading:
UITableViewController Class Reference
Table View Programming Guide for iOS
UITableViewDelegate Protocol Reference

Adding a TableViewController to an existing table view

I've got a Table view and it's the child of the View ( see the IB hierarchy ) :
I want to start customizing the tableCells, so I assume I'll have to add a tableViewController . Where would I start in this instance ? the TableViewcontroller can't be a child of the MainViewController, can it ?
Cheers,
You would need to add a table view controller at the top level.
Then drag connections from the table to the controller of datasource and delegate
Then drag connections from the controller to the view for "view"
Then change the class of the controller you added (it's the last tab in the inspector) to your custom UITableViewController class (if you don't subclass it it does nothing)
Then make sure that you somehow retain the controller when the nib is loaded. Easiest way is with an IBOutlet in the file's owner.
Sorry if this is a bit general, but the specifics would consume pages. I don't recommend this approach unless you have some experience with this and know what most of the above means, in which case it's possible and sometimes desirable to do this, and good job if you do.
The easiest method, if you just want a simple table view, no special circumstances, is to use the File>New File>UITableViewController with xib for user interface, then go from there with the file it makes, but that isn't your question, so I have answered as best I can.
It would be easier to build a tableViewController class with a table inside that and add the tableViewController to the view instead of reverse-engineering it. The tableViewController will have all your delegate and datasource functions for your table fairly mapped out so you can move whatever you already have into those functions.
As for custom tableViewCells, here's a great tutorial from pragmaticstudio.com on how to do so. He walks you through all the aspects of building a tableViewController and also how to customize your cells in IB.

I need help using UITableView Controller sub class. How should i populate the tableview with my values?

I am working on a view-based project. My first view is a UIView where i fetch my data. Then i add a subview to my window which is a UITableView. Now i am unable to populate my data to UITableView. Please help..
There are two properties in UITableView, that are called delegate and dataSource. They can be bind on any object that implements specific protocol (UITableViewDelegate and UITableViewDataSource). Second object (DataSource) is what you need. It has all the callbacks for populating table rows/sections.
By default these properties are bind to a UITableViewController instance. But you can change that.
This is not something that can be easily answered in a paragraph or two. You would do well to thoroughly read through Apple's Table View Programming Guide to understand how the UITableView and its data controller and delegate methods are used. This is an excellent overview of how this class works.