Solution to the problem of uitableview sliding gesture conflict on iOS plus Uiscrollview

Source: Internet
Author: User

In the UITableView inside realizes the cell the left slippery deletion function is quite simple, generally everybody will do. However, if the UITableView is added to the Uiscrollview, a series of problems will arise.
The first reason is that because the UITableView list is too wide to exceed the width of the screen, it has to be added to the Uiscrollview to control the left and right sliding of the Uiscrollview contentsize implementation list.

In general, our user experience is the hope that the table is close to the screen border, not to allow users to see the screen superfluous out of the part, this time will be the Uiscrollview properties bounces to shut down, set to No, that is, the so-called spring effect.
Now let's talk about the problems that arise when adding UITableView to Uiscrollview:

Some items require a left-slip removal feature for UITableView, but the UITableView list is added to the Uiscrollview, and if you slide to the left, you'll find that you can't see the left-sliding button, but sometimes it comes out, I think it may be this time the gesture slip response in the UITableView body, which is the problem of sliding gesture conflict. After some access to information, finally found a solution, the way is to rewrite the Uiscrollview class, inherit Uiscrollview.
MyScrollview.h file

#import <UIKit/UIKit.h>

@interface myscrollview:uiscrollview<uigesturerecognizerdelegate>

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer;

@end
MYSCROLLVIEW.M file


#import "MyScrollview.h"

@implementation Myscrollview

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer {
if (gesturerecognizer.state!= 0) {
return YES;
} else {
return NO;
}
}

@end
Then you can import #import "MyScrollview.h" in the main interface Viewcontroller and use Myscrollview instead of Uiscrollview.
Enclosed is a small demo code I wrote:

#import "ViewController.h"
#import "SideslipTableViewCell.h"
#import "MyScrollview.h"

@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate,uigesturerecognizerdelegate>{
UITableView *sidesliptableview;
Nsmutablearray *dataarray;
Myscrollview *mainscrollview;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

[Self initui];
DataArray = [Nsmutablearray arraywitharray: @[@ "1111", @ "2222", @ "3333", @ "4444", @ "5555", @ "6666", @ "7777", @ "8888", @ " 9999 "]];
}
#pragma mark-Initialize UI
-(void) initui{
Self.view.backgroundColor = RGB (242, 242, 247);
Self.automaticallyadjustsscrollviewinsets = NO;
Mainscrollview = [[Myscrollview alloc] Initwithframe:cgrectmake (0, +, Kscreenwidth, kscreenheight-44-10)];
Mainscrollview.bounces = NO;
Mainscrollview.delegate = self;
[Mainscrollview Setdelayscontenttouches:no];
[Mainscrollview Setcancancelcontenttouches:no];
[Self.view Addsubview:mainscrollview];

Sidesliptableview = [[UITableView alloc] Initwithframe:cgrectmake (0, 440, kScreenHeight-60) style: Uitableviewstyleplain];
Sidesliptableview.backgroundcolor = [Uicolor Clearcolor];
Sidesliptableview.delegate = self;
Sidesliptableview.datasource = self;
Sidesliptableview.separatorstyle = Uitableviewcellseparatorstylenone;
[Mainscrollview Addsubview:sidesliptableview];

Mainscrollview.contentsize = Cgsizemake (440, 0);
}

#pragma mark-Okay.
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
return dataarray.count;
}

#pragma mark-row height
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
return 46;
}

#pragma mark-cell content
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath{
static NSString *indefier = @ "Cell";
Sidesliptableviewcell *cell = [TableView dequeuereusablecellwithidentifier:indefier];
if (!cell) {
cell = [[Sidesliptableviewcell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:indefier];
}
Cell.selectionstyle = Uitableviewcellselectionstylenone;
Cell.lable.text = Dataarray[indexpath.row];
return cell;
}

Let's set up a cell to edit.
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath
{
return YES;
}

Define edit Style
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (NSIndexPath *) Indexpath
{
return uitableviewcelleditingstyledelete;
}

Go to edit mode, press the Edit button that appears, and then delete the operation
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath
{
if (Editingstyle = = Uitableviewcelleditingstyledelete) {
[DataArray RemoveObjectAtIndex:indexPath.row];
Delete the row from the data source.
[Sidesliptableview Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationfade];
}
}

Modify Edit button text
-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (NSIndexPath *) Indexpath
{
return @ "Delete";
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}

@end
Here is the function of the left-slip removal, if you need to implement a left-slip appearance of multiple buttons, such as: Delete and close, the above method-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) Indexpath commented out, and then implement the following method:

-(Nsarray *) TableView: (UITableView *) TableView Editactionsforrowatindexpath: (Nsindexpath *) IndexPath {
Uitableviewrowaction *deleterowaction = [uitableviewrowaction rowactionwithstyle: uitableviewrowactionstyledestructive title:@ "Delete" handler:^ (uitableviewrowaction *action, NSIndexPath *indexPath) {// Title can be self defined
NSLog (@ "click Delete");
}];
Deleterowaction.backgroundcolor = RGB (210, 207, 209);
Here is iOS8.0 after Apple's newest api,uitableviewrowaction,style is the definition of the label color and so on, which can also be defined
Uitableviewrowaction *editrowaction = [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@ "Close" handler:^ (uitableviewrowaction *action, Nsindexpath *indexpath) {
NSLog (@ "click off");
}];
Editrowaction.backgroundcolor = [Uicolor redcolor];//can define the color of rowaction
Return @[editrowaction,deleterowaction];//finally returns an array of these two rowaction
}

Note: The following two attributes must be added:


[Mainscrollview Setdelayscontenttouches:no];
[Mainscrollview Setcancancelcontenttouches:no];
//Current screen width
#define Kscreenwidth [UIScreen mainscreen].bounds.size.width
//Current screen height
#define Kscreenheight [ UIScreen mainscreen].bounds.size.height

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.