UIWebView and UITableView scroll at the same time - ios

I have a UIWebView in my UITableView cell. However when I scroll the UIWebView, UITableView scrolls too. How do I make sure only one UIWebView scrolls when the user tries to scroll it?

This Question has not been answered yet so i am posting my answer so
late .
I had the same problem , i solved with creating custom Delegate. follow the steps below.
1. Create Custom delegate in your UITableViewCell
#protocol MyTableCellDelegate <NSObject>
-(void)webViewDidStartedScrolling;
-(void)webViewDidStoppedScrolling;
#end
2. Add UIScrollViewDelegate in your TableCell.h file
#interface TableCell : UITableViewCell<UIWebViewDelegate,UIScrollViewDelegate>
property(weak,nonatomic) id<MyTableCellDelegate> delegateWebView;
3. in your TableCell.m file
- (void)awakeFromNib {
[super awakeFromNib];
self.myWebView.scrollView.delegate=self;
}
//ScrollView delegate method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if(self.delegateWebView && [self.delegateWebView respondsToSelector:#selector(webViewDidStoppedScrolling)]){
[self.delegateWebView webViewDidStoppedScrolling];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if(self.delegateWebView && [self.delegateWebView respondsToSelector:#selector(webViewDidStartedScrolling)]){
[self.delegateWebView webViewDidStartedScrolling];
}
}
4. add MyTableCellDelegate in your UIViewController.h where you are having Your table View.
#interface UIViewController ()<UITableViewDelegate,UITableViewDataSource,MyTableCellDelegate>
5. Implement the methods of your custom delegate methods in your UIViewController.m file , and enable or disable tableView scrolling , when your webviewDidEndScrolling and webviewDidStartedScrolling
#pragma mark UItableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
YourTableCell *cell=[tableView dequeueReusableCellWithIdentifier:#"YourTableCell" forIndexPath:indexPath];
cell.delegateDealDetails=self;
}
// Custom Delegate Callback method
-(void)webViewDidStartedScrolling
{
tblDealDetails.scrollEnabled=NO;
}
-(void)webViewDidStoppedScrolling
{
tblDealDetails.scrollEnabled=YES;
}
This works fine for me , i hope this will help others who is facing same problem.

You can use an disable and enable scroll of the tableView, when the webView scroll delegate is called.
UIWebView has a scrollView inside, that you can be I'ts delegate.
- (void)setupSwip
{
UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwip:)];
swipeGesture.delegate = self;
[self.view addGestureRecognizer:swipeGesture];
self.tableView.scrollEnabled = YES
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
self.tableView.scrollEnabled = NO;
return YES;
}
- (void)didSwip:(UIGestureRecognizer*)gestureRecognizer
{
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.tableView.scrollEnabled = NO;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate)
{
self.tableView.scrollEnabled = YES;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.tableView.scrollEnabled = YES;
}

Related

UIbutton should not scroll with view [duplicate]

Basically, I wanna create a static UIButton on top whether or not the table view is scrolled. However, I tried to add the UIButton as the subview of the UIView and make it on the top by using "bringSubviewToFront" method. However, the UIButton still moves when I scroll the UITableView. Therefore, how can I make a static UIButton overlaying the UITableView?
Here is my code:
#import "DiscoverTimelineTableViewController.h"
#interface DiscoverTimelineTableViewController ()
#property (strong, nonatomic) UIView* myview;
#end
#implementation DiscoverTimelineTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self displayMakePlanButton];
NSLog(#"%f", self.view.layer.zPosition);
NSLog(#"%f", self.tableView.layer.zPosition);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = #"Sleepy";
return cell;
}
#pragma mark - Add the make plan button
- (void) displayMakePlanButton
{
CGFloat buttonWidth = self.view.frame.size.width;
CGFloat buttonHeight = 104.0f;
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat statBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIButton *makePlanButton = [UIButton buttonWithType: UIButtonTypeCustom];
[makePlanButton setFrame:CGRectMake(0, self.view.frame.size.height - buttonHeight - navBarHeight - statBarHeight, buttonWidth, buttonHeight)];
[makePlanButton setBackgroundColor:[UIColor colorWithRed:0.286 green:0.678 blue:0.231 alpha:1]];
[makePlanButton setTitle:#"MAKE PLAN" forState:UIControlStateNormal];
[self.view addSubview:makePlanButton];
//makePlanButton.layer.zPosition = 1;
[self.view bringSubviewToFront:makePlanButton];
NSLog(#"%f", makePlanButton.layer.zPosition);
}
#end
You're using a table view controller, so self.view is the table view. A table view is a scroll view, so the view scrolls along with everything else.
You should use a regular view controller with a table as a subview instead.
Alternatively, you could try resetting the view's frame in the scrollViewDidScroll: scroll view delegate method, but I think the view would still jitter a bit.
Finally, you could add the button directly to the UIWindow, but that's likely to bring up a whole host of other problems with rotations, animations, and transitions.

UITextField show keyboard on tableviewcell click

I have a simple custom table view cell that has a label and a textfield. Looks like this in the storyboard:
I would like to show the keyboard when the user clicks anywhere in the cell, including if they click the label. I was 99% sure the way to achieve this would be to call becomeFirstResponder when the cell is clicked.
Here is my simple ViewController:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView setEstimatedRowHeight:44.0f];
[self.tableView setRowHeight:UITableViewAutomaticDimension];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"custom"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];
}
And my custom table view cell:
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (BOOL) becomeFirstResponder {
return [self.textField becomeFirstResponder];
}
#end
I verified that becomeFirstResponder is called, however that is returning false. What am I missing?
Think as #alex-i points out in a comment here:
This [text field not becoming first responder] can also occur when the textfield is briefly removed from the
view/window hierarchy while becoming the first responder (e.g.
reloading a UITableView or UICollectionView that holds that
textfield).
Which will happen on selection.
Rather than use didSelectRowAtIndexPath, you can add a UITapGestureRecognizer to your tableView with an action like:
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL firstResponder = [cell becomeFirstResponder];
}
And it will become first responder.
If someone needs a swift alternative: You can connect outer UIViews with outlet collection. On each UIViewController class you have once call below. So that when the outer box is touched, editfield will be activated with keyboard. You can apply this to your labels as well. (By disabling label's User Interaction)
#IBOutlet var outerBoxes:[UIView]!
override func viewDidLoad() {
super.viewDidLoad();
for outerBox in outerBoxes { outerBox.respondForEditBox() };
...
But once you have to have this code:
extension UIView {
func respondForEditBox() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIView.focusOnEditBox));
self.addGestureRecognizer(tap);
}
func focusOnEditBox() {
for sview in self.subviews {
if sview is UITextField {
sview.becomeFirstResponder();
}
}
}
}
Inspired by #Esqarrouth's answer at
Close iOS Keyboard by touching anywhere using Swift

How to Put a static UIButton Overlaying a UITableView

Basically, I wanna create a static UIButton on top whether or not the table view is scrolled. However, I tried to add the UIButton as the subview of the UIView and make it on the top by using "bringSubviewToFront" method. However, the UIButton still moves when I scroll the UITableView. Therefore, how can I make a static UIButton overlaying the UITableView?
Here is my code:
#import "DiscoverTimelineTableViewController.h"
#interface DiscoverTimelineTableViewController ()
#property (strong, nonatomic) UIView* myview;
#end
#implementation DiscoverTimelineTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self displayMakePlanButton];
NSLog(#"%f", self.view.layer.zPosition);
NSLog(#"%f", self.tableView.layer.zPosition);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = #"Sleepy";
return cell;
}
#pragma mark - Add the make plan button
- (void) displayMakePlanButton
{
CGFloat buttonWidth = self.view.frame.size.width;
CGFloat buttonHeight = 104.0f;
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat statBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIButton *makePlanButton = [UIButton buttonWithType: UIButtonTypeCustom];
[makePlanButton setFrame:CGRectMake(0, self.view.frame.size.height - buttonHeight - navBarHeight - statBarHeight, buttonWidth, buttonHeight)];
[makePlanButton setBackgroundColor:[UIColor colorWithRed:0.286 green:0.678 blue:0.231 alpha:1]];
[makePlanButton setTitle:#"MAKE PLAN" forState:UIControlStateNormal];
[self.view addSubview:makePlanButton];
//makePlanButton.layer.zPosition = 1;
[self.view bringSubviewToFront:makePlanButton];
NSLog(#"%f", makePlanButton.layer.zPosition);
}
#end
You're using a table view controller, so self.view is the table view. A table view is a scroll view, so the view scrolls along with everything else.
You should use a regular view controller with a table as a subview instead.
Alternatively, you could try resetting the view's frame in the scrollViewDidScroll: scroll view delegate method, but I think the view would still jitter a bit.
Finally, you could add the button directly to the UIWindow, but that's likely to bring up a whole host of other problems with rotations, animations, and transitions.

UISearchBar - ReturnKeyType not working for iOS 8

I am using UISearchbar in tableview controller in storyboard.
And searchbar returnKeyType is UIReturnKeySearch.
Its working fine with iOS7 but returnKeyType is not working with iOS8.
in iOS8, return key appears every time in keyboard.
I tried to set returnkeytype in viewDidLoad method of controller too.
What I need to do to set returnKeyType = UIReturnKeySearch in iOS8?
I think you can go with your hard codded logic for right now.
I will update if I will get better solution for your problem.
-(void)viewDidLoad {
[self setReturnKeyTypeSearchForView:searchBar];
}
-(void)setReturnKeyTypeSearchForView:(UIView *)view
{
for (id subView in view.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
[subView setReturnKeyType:UIReturnKeySearch];
}
else {
[self setReturnKeyTypeSearchForView:subView];
}
}
if ([view isKindOfClass:[UITextField class]]) {
[(UITextField *)view setReturnKeyType:UIReturnKeySearch];
}
}
Try making IBOutlet of your SearchBar
#property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
and add the below line code to your viewDidLoad Method
// if u want Done return key and change accordingly.
_searchBar.returnKeyType = UIReturnKeyDone;
SearchViewController.h
//
#import <UIKit/UIKit.h>
#interface SearchViewController : UIViewController
<UISearchBarDelegate, UITableViewDataSource> {
NSMutableArray *tableData;
UIView *disableViewOverlay;
UITableView *theTableView;
UISearchBar *theSearchBar;
}
#property(retain) NSMutableArray *tableData;
#property(retain) UIView *disableViewOverlay;
#property (nonatomic, retain) IBOutlet UITableView *theTableView;
#property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;
#end
SearchViewController.m
//
#import "SearchViewController.h"
#implementation SearchViewController
#synthesize tableData;
#synthesize disableViewOverlay;
#synthesize theSearchBar;
#synthesize theTableView;
// Initialize tableData and disabledViewOverlay
- (void)viewDidLoad {
[super viewDidLoad];
self.tableData =[[NSMutableArray alloc]init];
self.disableViewOverlay = [[UIView alloc]
initWithFrame:CGRectMake(0.0f,44.0f,320.0f,416.0f)];
self.disableViewOverlay.backgroundColor=[UIColor blackColor];
self.disableViewOverlay.alpha = 0;
}
// Since this view is only for searching give the UISearchBar
// focus right away
- (void)viewDidAppear:(BOOL)animated {
[self.theSearchBar becomeFirstResponder];
[super viewDidAppear:animated];
}
#pragma mark -
#pragma mark UISearchBarDelegate Methods
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
// We don't want to do anything until the user clicks
// the 'Search' button.
// If you wanted to display results as the user types
// you would do that here.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
// searchBarTextDidBeginEditing is called whenever
// focus is given to the UISearchBar
// call our activate method so that we can do some
// additional things when the UISearchBar shows.
[self searchBar:searchBar activate:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
// searchBarTextDidEndEditing is fired whenever the
// UISearchBar loses focus
// We don't need to do anything here.
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
// Clear the search text
// Deactivate the UISearchBar
searchBar.text=#"";
[self searchBar:searchBar activate:NO];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Do the search and show the results in tableview
// Deactivate the UISearchBar
// You'll probably want to do this on another thread
// SomeService is just a dummy class representing some
// api that you are using to do the search
NSArray *results = [SomeService doSearch:searchBar.text];
[self searchBar:searchBar activate:NO];
[self.tableData removeAllObjects];
[self.tableData addObjectsFromArray:results];
[self.theTableView reloadData];
}
// We call this when we want to activate/deactivate the UISearchBar
// Depending on active (YES/NO) we disable/enable selection and
// scrolling on the UITableView
// Show/Hide the UISearchBar Cancel button
// Fade the screen In/Out with the disableViewOverlay and
// simple Animations
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active{
self.theTableView.allowsSelection = !active;
self.theTableView.scrollEnabled = !active;
if (!active) {
[disableViewOverlay removeFromSuperview];
[searchBar resignFirstResponder];
} else {
self.disableViewOverlay.alpha = 0;
[self.view addSubview:self.disableViewOverlay];
[UIView beginAnimations:#"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
self.disableViewOverlay.alpha = 0.6;
[UIView commitAnimations];
// probably not needed if you have a details view since you
// will go there on selection
NSIndexPath *selected = [self.theTableView
indexPathForSelectedRow];
if (selected) {
[self.theTableView deselectRowAtIndexPath:selected
animated:NO];
}
}
[searchBar setShowsCancelButton:active animated:YES];
}
#pragma mark -
#pragma mark UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"SearchResult";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
id *data = [self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = data.name;
return cell;
}
#pragma mark -
#pragma mark Memory Management Methods
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[theTableView release], theTableView = nil;
[theSearchBar release], theSearchBar = nil;
[tableData dealloc];
[disableViewOverlay dealloc];
[super dealloc];
}
#end
Building a SearchView with UISearchBar and UITableView
this might helps you :)
I'm not sure if I understood your question correctly. You want to have "search" button instead of "return" button, right? There is a new SearchController in ios 8, give it a try:
YourTableViewController.h
#interface YourTableViewController : UITableViewController<UISearchResultsUpdating>
#end
And now the implementation:
YourTableViewController.m
- (void)viewDidLoad {
// initializing with the same controller as presenting
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44.0f);
searchController.dimsBackgroundDuringPresentation = NO;
searchController.searchBar.delegate = self;
searchController.searchBar.returnKeyType = UIReturnKeySearch; //should be search by default.. you can change to whatever you want.
// adding searchBar into HeaderView
self.tableView.tableHeaderView = searchController.searchBar;
// just to be able to present results on the same controller
self.definesPresentationContext = YES;
}
You also have to implement method from UISearchResultsUpdating protocol:
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// you can leave it blank
}
EDIT: If it is not what you were looking for please comment, so I can update my answer accordingly
try this in viewDidLoad:
UITextField *txfSearchField = [yourSearchbar valueForKey:#"_searchField"];
if([txfSearchField conformsToProtocol:#protocol(UITextInputTraits)]) {
[txfSearchField setReturnKeyType:UIReturnKeyDefault];
}

Enable and Disable UITableview scroll based on UITableview cell movement on dragging

I am creating one application based on FMMoveTableView where I have to drag cell on long press and change its position with in same section and different section.The cell is dragging fine and setting in the same and different section.But the problem is when I start dragging the cell upwards the table also starts scrolling up.So some of its cells are invisible because of bounce where we want to keep the dragged cell.The same thing is happening when I drag the cell to the bottom.
Is it anything related to UITableView property or I have to do it programmatically?
The app FMMoveTableView which I followed for this functionality,it is working fine where it is using UITableView class type.I implemented it in UIViewController class where I made some other views.
UITableView Properties:
self.GroupedTableView=[[UITableView alloc]initWithFrame:CGRectMake(20, 25, 280, 480) style:UITableViewStylePlain];
self.GroupedTableView.showsHorizontalScrollIndicator=YES;
self.GroupedTableView.showsVerticalScrollIndicator=YES;
self.GroupedTableView.bounces=YES;
self.GroupedTableView.alwaysBounceHorizontal=NO;
self.GroupedTableView.alwaysBounceVertical=YES;
self.GroupedTableView.bouncesZoom=YES;
self.GroupedTableView.delaysContentTouches=YES;
self.GroupedTableView.canCancelContentTouches=YES;
self.GroupedTableView.userInteractionEnabled=YES;
self.GroupedTableView.dataSource=self;
self.GroupedTableView.delegate=self;
self.GroupedTableView.rowHeight=30;
self.GroupedTableView.backgroundColor=[UIColor clearColor];
self.GroupedTableView.tag=202;
[self.view addSubview:self.GroupedTableView];
Long Press Gesture:
UILongPressGestureRecognizer *movingGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
[movingGestureRecognizer setDelegate:self];
[self.GroupedTableView addGestureRecognizer:movingGestureRecognizer];
Auto Scroll Methods:
- (void)legalizeAutoscrollDistance
{
float minimumLegalDistance = [self.GroupedTableView contentOffset].y * -1;
float maximumLegalDistance = [self.GroupedTableView contentSize].height - ([self.GroupedTableView frame].size.height + [self.GroupedTableView contentOffset].y);
[self setAutoscrollDistance:MAX([self autoscrollDistance], minimumLegalDistance)];
[self setAutoscrollDistance:MIN([self autoscrollDistance], maximumLegalDistance)];
}
- (void)stopAutoscrolling
{
[self setAutoscrollDistance:0];
[[self autoscrollTimer] invalidate];
[self setAutoscrollTimer:nil];
}
- (void)maybeAutoscrollForSnapShotImageView:(FMSnapShotImageView *)snapShot
{
[self setAutoscrollDistance:0];
NSLog(#"Height====%f",[self.GroupedTableView frame].size.height);
NSLog(#"Height====%f",[self.GroupedTableView contentSize].height);
NSLog(#"Frame====%#",NSStringFromCGRect([snapShot frame]));
NSLog(#"Frame====%#",NSStringFromCGRect([self.GroupedTableView bounds]));
// Check for autoscrolling
// 1. The content size is bigger than the frame's
// 2. The snap shot is still inside the table view's bounds
if ([self.GroupedTableView frame].size.height < [self.GroupedTableView contentSize].height && CGRectIntersectsRect([snapShot frame], [self.GroupedTableView bounds]))
{
CGPoint touchLocation = [[self movingGestureRecognizer] locationInView:self.GroupedTableView];
touchLocation.y += [self touchOffset].y;
float distanceToTopEdge = touchLocation.y - CGRectGetMinY([self.GroupedTableView bounds]);
float distanceToBottomEdge = CGRectGetMaxY([self.GroupedTableView bounds]) - touchLocation.y;
if (distanceToTopEdge < [self autoscrollThreshold])
{
[self setAutoscrollDistance:[self autoscrollDistanceForProximityToEdge:distanceToTopEdge] * -1];
}
else if (distanceToBottomEdge < [self autoscrollThreshold])
{
[self setAutoscrollDistance:[self autoscrollDistanceForProximityToEdge:distanceToBottomEdge]];
}
}
if ([self autoscrollDistance] == 0)
{
[[self autoscrollTimer] invalidate];
[self setAutoscrollTimer:nil];
}
else if (![self autoscrollTimer])
{
NSTimer *autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0) target:self selector:#selector(autoscrollTimerFired:) userInfo:snapShot repeats:YES];
[self setAutoscrollTimer:autoscrollTimer];
}
}
- (void)autoscrollTimerFired:(NSTimer *)timer
{
[self legalizeAutoscrollDistance];
CGPoint contentOffset = [self.GroupedTableView contentOffset];
contentOffset.y += [self autoscrollDistance];
[self.GroupedTableView setContentOffset:contentOffset];
// Move the snap shot appropriately
FMSnapShotImageView *snapShot = (FMSnapShotImageView *)[timer userInfo];
[snapShot moveByOffset:CGPointMake(0, [self autoscrollDistance])];
// Even if we autoscroll we need to update the moved cell's index path
CGPoint touchLocation = [[self movingGestureRecognizer] locationInView:self.GroupedTableView];
[self moveRowToLocation:touchLocation];
}
- (float)autoscrollDistanceForProximityToEdge:(float)proximity
{
return ceilf(([self autoscrollThreshold] - proximity) / 5.0);
}
I am unable to stop tableview scroll when I drag a cell.What I need that table should not move till the dragged cell has not reached to the top or bottom and then it should scroll to show hidden Cells.
// // ViewController.h // testingApp
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UILongPressGestureRecognizer *reco; }
#property (nonatomic, weak) IBOutlet UITableView *table;
#end
//
// ViewController.m
// testingApp
#import "ViewController.h"
#implementation ViewController
#synthesize table;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
reco = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(recognize:)];
[self.table addGestureRecognizer:reco];
}
-(void)recognize:(id)sender
{
NSLog(#"recognize");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
return cell;
}
#end
I have got the solution.Actually I am using some gestures on my table view cell.So to enable this along with other gestures I was using:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
So this was actually activating the UITableView gestures too when it was not needed.And so When I dragged the cell Image my table also started scrolling along with the image.I misinterpreted it as my implementation for row sliding has got some issue.So code used in question works is fine if someone needs it in future.What I did is added some conditions in the above method and activated it when it was needed.

Resources