How to add cells to UITableView that has no controller? - iphone

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.

Related

Implementing UIScrollViewDelegate methods in UITableView

I am using a custom UITableView that complies to those protocols:
UITableViewDataSource
UIScrollViewDelegate
This table view is used in many places in my app. I have implemented image downloading in the background. When the user scrolls, additional images get downloaded – that's why I needed to implement the UIScrollViewDelegate methods. I definitely want to have this in this class and not in a view controller, because each of these custom table views use the same implementation of the methods.
Here is the problem. In my view controller, I need to implement the didSelectRowAtIndexPath method from UITableViewDelegate, because I need to perform a segue. But when I do that (and set the delegate to self), the UIScrollViewDelegate methods get caught by this view controller and are not propagated to the table view, because UITableView extends UIScrollView (and his delegate methods).
I have "solved" it by adding this to each of my view controller:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self.bookTableView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
But having this same block of code (FYI this is only one of them, there are some more) on many places throughout the app isn't very good. Do you have any ideas how I could solve this?
I definitely want to have this in this class and not in a view controller, because each of these custom table views use the same implementation of the methods.
No, you do not. UITableView is a view. It should never talk to the network. That is the function of the model (or possibly a model controller). The function you're discussing has nothing to do with displaying information. It has to do with fetching information.
What you want is to the scroll view delegate methods into a custom UITableViewController and subclass your other table view controllers from that. The actual downloads, however, should be managed by your model. Your view controllers should observe the model and update as it changes.
I have solved it by using the new feature in iOS 6 – embedded view controllers. In each view controller, where I'm using this table view controller, I perform this:
self.tableViewController.tableView = self.bookTableView;
[self addChildViewController:self.tableViewController];
Where self.bookTableView is the UITableView in my view.
To perform a segue from this child TVC, you can use this:
[self.parentViewController performSegueWithIdentifier:#"Book Details" sender:selectedBook];

How to display data in a table view that is added as IBoutlet to a view?

I have create a table view outlet and also drag a table view to my view.outlet is connected to table view. Now I am not able to display any data in the table view. How to do that....just want to access the table view. Thanks
To display data in the table, you must implement the UITableViewDataSource and UITableViewDelegate protocols. You can check any tutorial about that (this, for example).
You have to implement tableview data source and delegate methods.and keep in mind your class should be delegate and data source of your tableview.I mean after connecting outlet, you have to provide data source and delegate for tableview.May this link helps you:
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
In order to display data in your table you need to implement tableview datasource methods. Refer to this link:
UITableViewDataSource Protocol Reference
and set UITableView datasource property to your controller. If you want to perform some actions on a tableview, then you need to implement its delegate methods: Refer to this link:
UITableViewDelegate Protocol Reference

adding a static UITableView to a regular custom view

I have a custom view controller that has a view on the bottom half.
I would like to add a static UITableView on the top half.
So I dragged a UITableView on the view controller but apparently that is not allowed since static table views only are only embeddable in UIViewControllers.
I went to my code and made my controller extend UITableView but that doesn't fix the issue.
How do I add my static UITableView as a second view in my custom controller?
EDIT: Perhaps having a table view not taking up the whole screen is not very well supported in iOS storyboards. Maybe I will just use regular tables on a view since i just need 3 static rows.
You should be able to do something simple like this:
UITableViewController *tbv = [[UITableViewController alloc] initWithFrame:[CGRect whateverSize/Location]];
[self.view addSubview:tbv];
Be sure when doing this to also write needed delegate functions such as numberOfRowsInSection: , numberOfSections: , cellForRowAtIndexPath:, tableView:didSelectRowAtIndexPath:
Be sure to check out the UITableView Class Reference
I solved this issue by making my second view controller a simple UIViewController that implements the delegate and datasource, dragging a dynamic table on it and setting the rows and sections "statically" in code. Now I have my two views correctly cohabiting in a large view.

how to use a UITableView without a UITableViewController

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.

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.