Long press gesture on image in tableview custom cell - ios

I need some help. Today, I am working on table view custom cell where the cell contains an UIImageView. On the ImageView, I want to implement the long gesture. I implement code for this that is give below.. But I am doing something wrong in my code.In this the View is resize once on long press but i want after the some seconds it can be remove and come back in table view cell
Can anyone Suggest me????
Update:
Here's the code!
- (void)celllongpressed:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
cell = (ActivityFeedCell *)[gesture view];
}
if (gesture.state == UIGestureRecognizerStateChanged)
{
cell = (ActivityFeedCell *)[gesture view];
logGes_view=[[UIView alloc]initWithFrame:CGRectMake(5, 0,self.view.frame.size.width-10,self.view.frame.size.height)];
image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 80,self.view.frame.size.width, self.view.frame.size.height-80)];
image.image=cell.updated_imgView.image;
UILabel *name_label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 150, 30)];
//city_label.backgroundColor=[UIColor yellowColor];
name_label.text=lgGesNamelbl;
UILabel *city_label=[[UILabel alloc]initWithFrame:CGRectMake(10, 50, 180, 30)];
//city_label.backgroundColor=[UIColor yellowColor];
city_label.text=lgGesCitylbl;
[logGes_view addSubview:city_label];
[logGes_view addSubview:name_label];
[logGes_view addSubview:image];
logGes_view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:logGes_view];
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
// cell = (ActivityFeedCell *)[gesture view];
[logGes_view removeFromSuperview];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(celllongpressed:)];
[gesture1 setDelegate:self];
[gesture1 setMinimumPressDuration:1.0];
[ cell setUserInteractionEnabled:YES];
[cell addGestureRecognizer:gesture1];
}

UILongPressGestureRecognizer *reconizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
[reconizer setMinimumPressDuration:1.0];
[cell addGestureRecognizer:reconizer];
-(void)handleLongPress:(UILongPressGestureRecognizer*)reconizer
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
UITableViewCell *cell = (UITableViewCell *)[gesture view];
NSIndexPath *indexPath = [tableview indexPathForCell:cell];
NSString *s = [NSString stringWithFormat: #"row=%1ld",(long)indexPath.row];
[self setTitle: s];
}
if (gesture.state == UIGestureRecognizerStateChanged)
{
cell = (UITableViewCell *)[gesture view];
cell.updated_imgView.frame=CGRectMake(0, 0, tableview.frame.size.width, tableview.frame.size.height);
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
cell = (UITableViewCell *)[gesture view];
cell.updated_imgView.frame=CGRectMake(0, 0, 100, 100);
}
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}

[cell addGestureRecognizer:gesture1]
replace with below line
[ cell.yourimageview setUserInteractionEnabled:YES]; // This enable user interaction on the image view. Required!!
[cell.yourimageview addGestureRecognizer:gesture1]; //yourimageview is your image outlet

Try it:
Cell is superView of imageView so on size changing it will not cross cell frame
add it to mainView after resize.

Related

Calling uipangesture from uitableviewcell class when tapped a cell

I am using ABMenuTableViewCell tableview controller in my application. I want to call didSelectRowAtIndexPath when i swipe a UITableViewCell.
Right now didSelectRowAtIndexPath only execute when I tap on a cell, I want to call it even when I swipe it. here is my didSelectRowAtIndexPath and cellforRowAtIndexPath methods code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *likes;
UILabel *downloads;
static NSString *CellIdentifier = #"Cell";
ABMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ABMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"acc_arrow_back.png"]];
arrow.frame = CGRectMake(300, 50, 5, 12);
arrow.image = [arrow.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[arrow setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:arrow];
UIImageView *likes_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"social.png"]];
likes_img.frame = CGRectMake(15, 80, 15, 15);
likes_img.image = [likes_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[likes_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:likes_img];
likes =[[UILabel alloc]initWithFrame:CGRectMake(33, 78, 80, 20)];
likes.tag = 1001; // set a tag for this View so you can get at it later
likes.textColor=[UIColor darkGrayColor];
likes.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
likes.text=[[rssOutputData objectAtIndex:indexPath.row]xmllikes];
[cell.contentView addSubview:likes];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
UIImageView *downloads_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"download.png"]];
downloads_img.frame = CGRectMake(55, 79, 15, 15);
downloads_img.image = [downloads_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[downloads_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:downloads_img];
downloads =[[UILabel alloc]initWithFrame:CGRectMake(73, 78, 80, 20)];
downloads.tag = 1002; // set a tag for this View so you can get at it later
downloads.textColor=[UIColor darkGrayColor];
downloads.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
downloads.text=[[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
[cell.contentView addSubview:downloads];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}
else
{
// use viewWithTag to find lblNombre in the re-usable cell.contentView
likes = (UILabel *)[cell.contentView viewWithTag:1001];
downloads = (UILabel *)[cell.contentView viewWithTag:1002];
}
cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmlsinger];
cell.detailTextLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
// custom menu view
NSString *nibName = #"ABCellMailStyleMenuView";
ABCellMenuView *menuView = [ABCellMenuView initWithNib:nibName bundle:nil];
menuView.delegate = self;
menuView.indexPath = indexPath;
cell.rightMenuView = menuView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
And these are the methods in cell class
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer and
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture
here is when i swipe
You can modified swipeGesture method will following code. it will show UITableViewCell as selected once you perform swipe operation.
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSInteger direction;
// find swipe direction
CGPoint velocity = [gesture velocityInView:self];
if (velocity.x > 0) {
// towards right - hide menu view
direction = ABMenuUpdateHideAction;
}
else {
// towards left - show menu view
direction = ABMenuUpdateShowAction;
}
UITableView* tableView = (UITableView*)self.superview.superview;
CGPoint swipeLocation = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:swipeLocation];
[tableView selectRowAtIndexPath:swipedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self updateMenuView:direction animated:YES];
}
}
Hope this help you.
As you are trying to achieve behaviour not supported by ABMenuTableViewCell, you will need to edit it's source:
Add _tapGesture and _menuVisible instance variables:
#implementation ABMenuTableViewCell {
CGRect _rightMenuViewInitialFrame;
UIPanGestureRecognizer *_swipeGesture;
UITapGestureRecognizer *_tapGesture;
BOOL _menuVisible;
}
Implement -tapGesture: method:
- (void) tapGesture:(UITapGestureRecognizer*)gesture {
if (_menuVisible)
[self updateMenuView:ABMenuUpdateHideAction animated:YES];
else
[self updateMenuView:ABMenuUpdateShowAction animated:YES];
}
Add UITapGestureRecognizer inside -commonInit method:
- (void) commonInit {
_swipeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
_swipeGesture.delegate = self;
[self addGestureRecognizer:_swipeGesture];
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
_tapGesture.delegate = self;
[self addGestureRecognizer:_tapGesture];
}
Update _menuVisible inside -updateMenuView:animated::
- (void)updateMenuView:(ABMenuUpdateAction)action animated:(BOOL)animated {
...
switch (action) {
case ABMenuUpdateShowAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame) - initialWidth, .0, initialWidth, CGRectGetHeight(self.contentView.frame));
_menuVisible = YES;
break;
case ABMenuUpdateHideAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame), .0, .0, CGRectGetHeight(self.contentView.frame));
_menuVisible = NO;
break;
default:
break;
}
...
}
You won't be able to select cells, but as I understand it, you don't want to.
If you want the method to be called when swiping the cell, you should be using a UISwipeGestureRecognizer and not a UIPanGestureRecognizer.
In your viewDidLoad:, set up the gesture recognizer.
// Create a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
// Add it to the table view
[self.tableView addGestureRecognizer:recognizer];
Handle the swipe.
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
// Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
// Check if index path is valid
if (indexPath)
{
// Select the cell at the indexPath
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
That should call the tableView: didSelectRowAtIndexPath: method.

How to show a view created inside the custom tableview cell by swiping the row?

I have created the iPhone app which has the custom tableview cell and I also created a view in cellForRowAtIndexPath. Now, when i swiping the the cell in UITable the view that i created should be displayed in the particular cell and all other cell contents should be remained same.
My code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKCardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"RKCardCell"];
// Configure the cell...
if (cell == nil) {
cell = [[RKCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RKCardCell"];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeMethod:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGesture];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-320.0, 8.0, 300, 180)];
[view setBackgroundColor:[UIColor greenColor]];
cell.profileImage.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [descriptionArray objectAtIndex:indexPath.row];
//%%% I made the cards pseudo dynamic, so I'm asking the cards to change their frames depending on the height of the cell
cell.cardView.frame = CGRectMake(10, 5, 300, [((NSNumber*)[cardSizeArray objectAtIndex:indexPath.row])intValue]-10);
return cell;
}
Following code is for gesture swiping:
- (void)swipeMethod:(UIGestureRecognizer *)gestureRec
{
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(self.rkCardCell.cardView.frame, -320.0, 0.0);
}];
}
Thanks in advance.
It is good if you use the gesture recognizer with the table view.
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeTarget:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:_myTableView];
And your swipe target method like as follows.
- (IBAction)swipeTarget:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint swipePoint = [gestureRecognizer locationInView:_myTableView];
NSIndexPath *cellIndexPath = [_myTableView indexPathForRowAtPoint:swipePoint];
RKCardCell *cell = [_myTableView cellForRowAtIndexPath:cellIndexPath];
// Add your code here to show your view
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(cell.cardView.frame, -320.0, 0.0);
}];
}
Take a look on https://github.com/CEWendel/SWTableViewCell .
It's a really good and simple library to make custom swipable tableviewcell.

Making UITextField in prototype cell first responder

I have set up the textfield with a tag and user interaction disabled in a prototype cell.
This is how I have created the cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
..
..
..
UILongPressGestureRecognizer *longPressgesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressgesture:)];
[longPressgesture setMinimumPressDuration:2.0];
[cell.contentView addGestureRecognizer: longPressgesture];
UITextField *toDoTextField = (UITextField *)[cell.contentView viewWithTag:1];
toDoTextField.tag = indexPath.row;
return cell;
}
then I would like to edit the textfield after a long press. So I have this:
- (void)LongPressgesture:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
}
else {
UITableView* tableView = (UITableView*)self.view;
CGPoint touchPoint = [recognizer locationInView:self.view];
NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
NSInteger rowRow = [row row];
UITextField *toDoTextField = (UITextField *)[cell.contentView viewWithTag:rowRow];
toDoTextField.userInteractionEnabled = YES;
[toDoTextField becomeFirstResponder];
}
When I trigger the long press though only the last cell will work. Not sure what I'm doing wrong.
I would like to be able to long press on any cell and then edit the text.
Thank you if you can help.
Add the long press gesture recognizer to the table view not the cell:
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
recognizer.minimumPressDuration = 2.0;
[self.myTableView addGestureRecognizer:recognizer];
Then handle it like this:
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (recognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (!indexPath)
NSLog(#"Long press was on tableView, but not on a row");
else
{
NSLog(#"Long press on table view # row %d", indexPath.row);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSAssert(cell, #"Cell must be found");
if (!cell)
return;
UITextField *toDoTextField =
(UITextField *)[cell.contentView viewWithTag:indexPath.row];
NSAssert(toDoTextField, #"UITextField must be found by tag");
if (toDoTextField)
{
[self.tableView endEditing:YES];
toDoTextField.userInteractionEnabled = YES;
[toDoTextField becomeFirstResponder];
}
}
}

applying gesture on all UICollectionViewCells

in my app i have a UICollectionView. i want to apply to it's cells a long pressure gesture. i implemented that but when i run the app only the last cell works and the others do not respond. what it wrong? here is my code.
#interface
#property (nonatomic, strong) UILongPressGestureRecognizer *longPressure;
in view did load:
self.longPressure = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
self.longPressure.delegate = self;
[self.collectionView addGestureRecognizer:self.longPressure];
gesture handler:
- (void)handlePressue:(UILongPressGestureRecognizer*)gesture{
[[gesture.view viewWithTag:1] setBackgroundColor:[UIColor yellowColor]];
}
in collectionView:cellforitemAtIndexPAth:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
is there anything wrong?
you need to add gesture on the each cell not for the UICollection View. that might be your problem.the last cell works because the gesture is added to the last cell only.
You need to creat long press gesture for each cell. Because gesture like other ui control,only one in the project.If you want add it to other view,you need copy another one.
Have a look at following Code snippet
MyFlowLayout *myLayout = [[MyFlowLayout alloc]init];
[self.collectionView setCollectionViewLayout:myLayout animated:YES];
UIGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
}
initWithTarget:self action:#selector(handlePinch:)];
[self.collectionView addGestureRecognizer:pinchRecognizer];
Handling the Pinch:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)sender {
// Get a reference to the flow layout
MyFlowLayout *layout =
(MyFlowLayout
*)self.collectionView.collectionViewLayout;
// If this is the start of the gesture
if (sender.state == UIGestureRecognizerStateBegan) {
// Get the initial location of the pinch?
CGPoint initialPinchPoint =
[sender locationInView:self.collectionView]; //Convert pinch location into a specific cell
NSIndexPath *pinchedCellPath =
[self.collectionView
indexPathForItemAtPoint:initialPinchPoint];
// Store the indexPath to cell
layout.currentCellPath = pinchedCellPath; }
else if (sender.state == UIGestureRecognizerStateChanged) {
// Store the new center location of the selected cell layout.currentCellCenter =
[sender locationInView:self.collectionView]; // Store the scale value
layout.currentCellScale = sender.scale; }
else {
[self.collectionView performBatchUpdates:^{ layout.currentCellPath = nil;
layout.currentCellScale = 1.0;
} completion:nil];
} }
For performance reasons I suggest you to avoid adding subviews and/or gestures to every single cell. Instead, add a singe longPressureGesture to your collectionView and on the gesture selector handle your per-cell switch business logic. Something as follow:
On your viewDidLoad or where you setup your collection, add the following gesture:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showDeleteActions:)];
longPress.delegate = self;
[_aCollectionView addGestureRecognizer:longPress];
then on the selector do the single-cell handling:
- (void)showDeleteActions:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSIndexPath *indexPath = [_aCollectionView indexPathForItemAtPoint:[gesture locationInView:_aCollectionView]];
UICollectionViewCell *cell = [_aCollectionView cellForItemAtIndexPath:indexPath];
NSLog(#"cell to delete at IndexPath: %#", indexPath);
}
}
this approach is much more efficient abd far more stable.
Here what it is causing that your UILongPressGestureRecognizer getting overwrite to last cell. So you need to create UILongPressGestureRecognizer for each cell in cellForRowAtIndexPath:
Here is a small code snippet that can help you out
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
UILongPressGestureRecognizer *longPressure =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
longPressure.delegate = self;
cell.userInteractionEnabled = YES; // Make userInteractionEnabled enable so it can detect touch
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
There is no need to create any property for the UILongPressGestureRecognizer in .h file. And also remove code to add userInteractionEnabled on whole UICollectionView.

Hide a label of a cell in a table view in iOS

I want to hide a label of a cell in a tableview.
(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
}
}
This code is hiding the label in the last cell as I failed to specify the code to hide the swiped cell's label.
Help to specify the swiped cell label to hide.
displayLabel.hidden = TRUE;
I need a replacement for this code.
You can try below code its working perfectly on my side:
- (void)viewDidLoad
{
UISwipeGestureRecognizer *recog = [[UISwipeGestureRecognizer alloc]initWithTarget:self
action:#selector(handleSwipeRight:)];
recog.delegate = self;
[recog setDirection:UISwipeGestureRecognizerDirectionRight];
[testTable addGestureRecognizer:recog];
// add the swipe gesture recognizer to tableview;
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
UILabel *aLabel = [[[UILabel alloc]init]autorelease];
aLabel.frame = CGRectMake(5, 0, 100, 40);
aLabel.text = [NSString stringWithFormat:#"aLabel %d",indexPath.row+1];
aLabel.tag = 1;//tag the labels
[cell.contentView addSubview:aLabel];
UILabel *bLabel = [[[UILabel alloc]init]autorelease];
bLabel.frame = CGRectMake(110, 0, 100, 40);
bLabel.text = [NSString stringWithFormat:#"bLabel %d",indexPath.row+1];
bLabel.tag = 2;//tag the label
[cell.contentView addSubview:bLabel];
UILabel *cLabel = [[[UILabel alloc]init]autorelease];
cLabel.frame = CGRectMake(215, 0, 100, 40);
cLabel.text = [NSString stringWithFormat:#"cLabel %d",indexPath.row+1];
cLabel.tag = 3;//tag the label
[cell.contentView addSubview:cLabel];
}
return cell;
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:testTable];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [testTable indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [testTable cellForRowAtIndexPath:indexPath];
for (id label in cell.contentView.subviews)
{
if ([label isMemberOfClass:[UILabel class]])
{
UILabel *referedLabel = (UILabel*)label;
if (referedLabel.tag == 2) //tag of bLabel;
{
referedLabel.hidden = YES;
}
}
}
}
}
I think displayLabel is pointing to label in last cell of your table. Where you are setting value to displayLabel.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
//You should get label from cell here. Currently displayLabel may be pointing to label in last cell, if I am correct you are assigning value to displaylabel in cellForRowAtIndexPath or in any other method.
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
Change your method to this:
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
// If you created a custom cell with a
// displayLabel property, change the pointer
// type from UITableViewCell to that type
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.displayLabel.hidden = YES;
// Use the following three lines instead, if your cell style is "Subtitle"
// cell.textLabel.hidden = YES;
// cell.imageView.hidden = YES;
// cell.detailTextLabel.hidden = YES;
[cell setNeedsDisplay];
}
}

Resources