how to write inside a cell of UITableView - objective-c

How do I write inside a cell of UITableView using TouchesBegan and TouchesEnd methods?

A UITableView scrolls and so cells move. What you are talking about would pretty much prohibit a table from being able to scroll. Of course you could have a static table or some other table which never scrolls and so you think that's ok.
In that case, handling touch events in a UITableViewCell is the same as handling the touch events in any other UIView. You could use a add a UIGestureRecognizer to the view and pull interaction that way or access the information through the UIResponder.
On thing to remember is, make sure userInteractionEnabled is set to YES if you are going straight through the UIResponder otherwise you'll just end up ignoring the events.

Related

Use single UITableViewCell subclass across multiple UIViewControllers

So I have this UITableViewCell subclass that has some really complicated logics in it - it triggers some actions in UIViewController it's actually attached to. Of course the cell is not aware of its UIViewController but I still navigate to it like this:
UITabBarController *tabVC = (UITabBarController *)appDelegate.rootVC.centerPanel;
SGFirstTabViewController *firstTab1 = [tabVC.viewControllers firstObject];
[firstTab1 reloadCell:self];
The thing is now that I want to use the same subclass of UITableViewCell around about 5 different UIViewControllers.
What's the best way to do this? I will almost never know what is the UITableViewCell's VC is and I simply can't create 5 different cell subclasses with the same code over and over. What's the best way around it?
I think there is perhaps some misunderstanding in the role of the cell in the model-view-controller programming pattern.
The cell should not reload itself but it should be told to reload by the view controller that controls it. The reload code in the cell can stay the same.
So, rather than the cell having to find out which one its view controller is, have the view controller listen to the cell action (e.g. via delegate methods) and fill it with the appropriate reload data as directed.

Prevent UITableView from scrolling when custom subview is handling touches

In my iOS app have a UITableView which contains a custom subview in one of it's cells. This cell is an interactive view that handles touch events (touchesBegan, touchesEnded, touchesMoved) to update itself.
The problem is that when the user 'drags' up or down, the tableView catches these touches (although I don't pass the touches up the responder chain), scrolls the table and prevents the subview from working correctly. I would like to prevent the table from scrolling as long as the user is touching that particular subview.
The subview has no reference at all to the tableView.
How can I prevent the scrolling behavior of the table?
Update
Despite accepting the answer below, I ended up handling my situation differently. I handle the touch events in my custom view now, pass them up the responder chain (to the table cell), the cell handles the touch events as well, using them to enable/disable scrolling on the superview (the table).
Turning off "Cancellable Content Touches" in the UITableView solved this for me (in the UITableView attributes inspector under Scroll View / Touch). I got this from this SO question: Scrolling a UITableView inside a UIScrollView
From the UIScrollView:canCancelContentTouches doc:
If the value of this property is NO, the scroll view does not scroll
regardless of finger movement once the content view starts tracking.
The most common method I use in such cases is to delegate information up the event chain. Set delegates in manner:
subview->cell->table
The second thing you can do is to send a notification via Notificaion Center. Table would listen to any events that forbid normal scrolling. It is overshoot but it will leave your code consistent.
I have no more ideas at the moment.

How to tell when a user taps in a UITableViewCell?

I'm working on an iOS app where I have UIWebviews inside UITableViewCells. The UIWebViews take up about 50% of the cell and the rest of the cell is blank. I need to be able to respond when a user taps in one of my cells. I've tried implementing the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, but this only responds when a user taps in a part of my cell that does NOT contain a uiwebview. Is there any way in which I can respond to a tap regardless of where in my cell the user taped?
you can set userInteractionEnabled property of your UIWebView to NO, but in this case you will not be able to scroll it..
Your design is definitely bad: Apple explicitly says in the UIWebView class reference, that you should not add UIWebViews to table cells. Also, as the web view internally manages an UIScrollView, which captures all the touches. So the only way to do what you want is to subclass UIWebView and override the touch management methods. This is also not advisable, as UIWebView is one of the few classes which are not to be subclassed according to Apple.
You have to seriously reconsider your design pattern.
First, do this to your webview. Then, call the tableView's didSelectRowAtIndexPath method when a tap is registered. Kind of a pain, but the only way I can think of. This will allow the user to interact with the webview in a completely normal way, but sends lets the table know which cell is being interacted with. If that's not what you want, the other answers should work.
Try attaching a UITapGestureRecognizer to your cell.

UITableView in a UIScrollView - How to make the view scroll, but not the TableView in itself?

Imagine, there is a UIViewController with a UIScrollView in it. At the top of the view there is an UIImageView, some UILabels and other things. Furthermore, there is a UITableView which content is Dynamic Prototypes. I attach a picture to make it clear:
I haven't got a static amount of cells in the UITableView so it could be scrollable. My problem is the following: the UITableView scrolls in itself but I want to scroll the whole View. What is the best possibility to do that?
Possible solutions I've founded today
1) The first thing is: I create a UITableViewController and declare a header section in which I include all my labels, images etc. programmatically (I would love to use the interface builder for that...)
2) Another solution is to calculate the height of the view. I tried the best to do it like this way - but: without success. If this is the best way to do that: Can anybody give an example?
I would ditch the UIScrollView and just use a UITableView. You can add a UIView object as the tableHeaderView of the UITableView just by dragging it in in Interface Builder. Now since everything is part of the UITableView hierarchy, everything will scroll together as expected.
You could also try setting delaysContentTouches to NO on your scrollView. Depending on your setup, this may make the scroll view respond to the touch first instead of the table view.
From Apples UIScrollView Docs:
delaysContentTouches
A Boolean value that determines whether the scroll view delays the
handling of touch-down gestures.
#property(nonatomic) BOOL delaysContentTouches
Discussion
If the value of this property is YES, the scroll view delays handling
the touch-down gesture until it can determine if scrolling is the
intent. If the value is NO , the scroll view immediately calls
touchesShouldBegin:withEvent:inContentView:. The default
value is YES.
You'll have to (as you've mentioned) add the UIView containing the image and buttons to the actual UITableView. Embedding it in the scroll view will produce the undesired behavior that you're seeing.
I would recommend returning the UIView as the header view for the first section of your table view. You can do this by implementing the UITableViewDelegate method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
If you maintain an IBOutlet to the view containing your image/labels, you can return it here.
this is same demo i hope its helps you from iphone sorce code library
http://developer.apple.com/library/ios/#samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html
thank you

Scrolling a UITableView inside a UIScrollView

I have a UITableView which is a subview of a UIView, then that UIView is a subview of a UIScrollView. How do I detect the touches that should scroll the UITableView?
The UITableView can get item selection events (a cell in the table is selected/tapped) just fine, except that you have to hold down on the cell before it fires. But I can't get the UITableView to scroll, its always the UIScrollView that reacts to the pan gesture.
Any help is greatly appreciated. Thanks in advance!
EDIT:
Solved, though I asked the wrong question. It does work by default as Roman K pointed out. I think the problem was related to having a part of the UITableView outside the bounds of the UIScrollView (the UITableView went over the bottom bounds of the UIScrollView). Setting it to correctly fit inside the UIScrollView fixed it.
Please, make sure that UIScrollView's properties delaysContentTouches and canCancelContentTouches are set appropriately. They control how UIScrollView instance passes touch information to its subviews. By default delaysContentTouches is set to YES. Also, make sure that, if you extended UIScrollView, touchesShouldBegin:withEvent:inContentView: allow touches in the subview.
Otherwise, UITableView scrolling should work by default in your scenario. If you create a test project with just the view hierarchy as described you will see that it is the case. So, compare the two and see what difference affects the scrolling.