Implementing UIScrollViewDelegate methods in UITableView - iphone

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];

Related

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.

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?

iPhone: viewWillAppear is not called for UIView

I have created an UIView in my iPhone app. I want to handle something when user closes or opens when UIView is present as current screen. I thought, i can do this under viewWillAppear:. But, viewWillAppear: is not called in UIView. Does it work only on UIViewController? How can i handle viewWillAppear: or viewDidAppear: for an UIView?
Update: UIView what I created everything through program, not in .xib.
Please advise.
Thanks!
From your message I infer that you wrote your viewWillAppear: method on the UIView class. As you suspect, that method is part of [UIViewController]1, not [UIView]2 therefore it only gets called on the UIViewController.
You should connect the property view of the UIViewController to the UIView object in the interface builder and then implement that method in the UIViewController.
If your view is created in response to an user action,
Update for your update:
You should tag the views either in code (view.tag=1) or IB.
Then you can do if (self.window.rootViewController.view.tag == 1) { ... } from your delegate (assuming you are looking for the view of the controller who is the rootController, otherwise post more details).
It's even better if you define constants on one place instead writing 1 as a literal.
These delegate methods are called every time the superview is presented to the screen and should be implemented in the UIViewControllers.
The gotcha is that these methods aren't called when subviews are presented on the screen, so your superview-view-controller will have to respond to these events accordingly.
You can find more information in this post here.
If you study the documentation for UIView and UIViewController what you will find is -(void)viewWillAppear:animated: is a method of UIViewController and not of UIView, so in order to use it, it must be implemented by subclassing UIViewController. Generally for best practice if you want to follow MVC, any functionality that does not pertain to the view itself should be delegated to the view controller and not be in the body of your UIView subclass.
Create a new view controller with xib file, and then link your custom view class to the view in your xib file.

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

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.