how to use a UITableView without a UITableViewController - iphone

I want to use a UITableView as a single selection in my own ViewController, now I can display it but fail to fill the cell or get wrong on how to fill the cell's textlabel. Any suggestion? ...

Implement the protocols for UITableViewDataSource, UITableViewDelegate in .h or .m then connect the tableview with the first responder -datasource and -delegate in interface builder and support the protocol required classes in your .m.
For datasource it is: cellForRowAtIndexPath and numbersOfRowsInSection.
Set your cell.textlabel.text in your cellForRowAtIndexPath method.

You have to register an object as its datasource if you want to fill it.
If you also want to receive events, you also have to implement a delegate

You can embed a TableViewController in your ViewController (property) and add the TableViewControllers view to your ViewControllers view (addSubView). Creating cells goes into the TableViewController and works the same way as in the usual case.

Related

How to add cells to UITableView that has no controller?

I want to add some data from a NSMutableArray to my UITableView. I have made an IBoutlet which is called lessonsTable. All the tutorials i can find assumes that the UITableView has its own controller, so they change a method about adding cells. But i don't have a separate controller class for my UITableView, it is just a view floating in another controller.
I've tried
[lessonsTable insertRowsAtIndexPaths:appDelegate.lessonsArray withRowAnimation:UITableViewRowAnimationTop];
But it didn't solved the problem.
lessonsArray has the data that i want to insert to my UITableView
You don't need a dedicated view controller, but you do need to implement the UITableViewDelegate and UITableViewDataSource protocols. You can't just send an array to a table and expect it to figure out the details for you.

UITableViewController without subclassing

Is it possible to create and display a UITableView controller which allows the user to select an item and fire back a message to the delegate without subclassing it?
The reason is I just want to display a list of items in a popovercontroller and it seems a waste to have to create a subclass just for this
In the view controller that presents the popover you could implement UITableViewDataSource and UITableViewDelegate - then set the popover's tableview to use the parent controller as its source and delegate before presenting it.
If you are using iOS 5 SDK then you can make static cells.
Otherwise the only option is to create a subclass and provide a DataSource array.
Either ways you might want to have a View Controller that prepares the view controller loaded on the touch action?

How to get access to UITableViewController properties when subclassing from UIViewController?

I have subclassed UIViewController to provide common functionality for all UIViewControllers (for example I'm overriding viewDidLoad method). My app uses a bunch of view controllers that are arranged inside tab bar controller and in navigation controllers. Everything is OK, except the fact I have one UITableViewController. I would like to subclass not it but my custom MyUIViewController. I'm able to get the table working by implementing data source and delegate protocols:
#interface MaschinenTableViewController : MyUIViewController <UITableViewDataSource, UITableViewDelegate>
However in this case, I do not have an access to UITableViewController properties. For example, I cannot change the behavior of table row selection because self is MyUIViewController not UITableViewController:
self.clearsSelectionOnViewWillAppear = YES;
Are there any workarounds for accessing those properties?
In this case you will need to add a UITableView variable to the header and set it up appropriately in viewDidLoad, and add it to your view. From this point it will work as a UITableViewController will (as essentially that's all it does!)
Take a look at my article here which takes this approach.
You could also subclass UITableViewController as MyUITableViewController, implementing the behavior you want, and then put a MyUITableViewController as variable to your MyUIViewController.
Did the same as Simon Lee mentioned using the delegate.
But without storing the index anywhere, at the end of didSelectRowAtIndexPath method called the deselectRowAtIndexPath. Worked for me no issues so far.

call a method in uitableivewcontroller from a method in custom uitableviewcell in the uitableivewcontroller

I need to call a method say
-(void)calculateValues
in uitableviewController from a custom- uitableviewcell method such as
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
.
The purpose is to refresh the values in other tableview cells based on the value entered through a textfield in this uitableivewcell. but how to access the uitableviewcontroller from the cell;self.superview will only point to the uitableview but i need to get the viewcontroller.how to get this?
You could use either a notification if its a few levels separated or a delegate pattern if you know about the view controller at creation time of the cell.

Does UITableViewController allow the table to be in a UIView?

UITableViewController seems to always hijack the View link in IB. So, if I put UITableView in a UIView and link up the View to that UIView, it still doesn't work. Only the UITableView is shown.
What I'd like to do is use a UITableViewController and put some labels on top of the uiTableView that can be hidden.. Like loading.. and No results found.
The only solution I have come up with is to resort to using UIViewController and then adding a UITableView link to the class and link it up in IB.
Am I missing something here?
It's fine to use a UIViewController, make it implement the table view datasource and delegate protocols, and then hook a UITableView up to it. It's also fine to have the controller's main view be a container UIView, and have a UITableView as a subview of that.
And yes, this is probably the best way to add some kind of overlay view, such as a message label. So I think you're on the right track.
You should also be able to do this using a UITableViewController, instead of a UIViewController that explicitly implements the table view protocols. I've had success with this. I'm not sure what you mean when you say that UITableViewController "hijacks" the view outlet in IB.
It really isn't a big deal either way. UITableViewController doesn't do much other than implement those protocols, provide a different default loadView method, and call [tableView reloadData] by default on viewWillAppear:. If you do those things yourself, you'll be fine.