1

I have an UIButton in a UITableViewCell of an UITableView. The UIButton is hidden. When the user swipes left with his finger on a specific UITableViewCell the button show up.

I use this code to implement it and it is working but the button shows up in more than one uitableviewcells other than the one that the user swiped his finger!

- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer 
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) 
    {
        UIView *tappedview=[gestureRecognizer.view hitTest:[gestureRecognizer locationInView:gestureRecognizer.view] withEvent:nil];

        UIView *contentview1=tappedview.superview;
        UIView *viewwithtag4=[contentview1 viewWithTag:7009];
        UIButton *button2=(UIButton *)viewwithtag4;

        NSLog(@"swipe left detected");

        [button2 setHidden:FALSE];
    }
}

Any help appreciated! Thanks.

1 Answer 1

0

if the button is showing up in the wrong cells after scrolling it is because the tableCells are being reused by the tableView in order to enhance performance. i.e.

If you want to keep the button visible for a particular cell, you will have to do the following:

In the method that is called by the gestureRecognizer save the button's state. You will have to determine the cell that has been swiped, then save that state probably in the class/model that your are filling the cell with. i.e your data source. For example, if your data source is an object in an array you could do something along the lines

// in your cellSwiped method and assuming you can traverse the view hierarchy to get
// the tableViewCell.
NSIndexPath *theCellIndexPath=[self.tableView indexPathForCell: theTableViewCell];
MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: theCellIndexPath.row];
// The buttonIsVisible ivar for your data source could be name that
// or something else that is meaningful.  Not sure what the button i
// related to in you objects
theDataSourceObject.buttonIsVisible=YES  // or you could put in code to toggle the value

Then in your cellForRowAtIndexPath method, you will have to set the button to be hidden or not depending on the state for that particular indexPath.

MyDataSourceObject *theDataSourceObject=[dataObjectArray objectAtIndex: indexPath.row];
cell.button.hidden=theDataSourceObject.buttonIsVisible;

return cell;

Good luck

4
  • I am saving the button of the cell in the dataObjectArray and then i call it from the dataObjectArray but the same happens!
    – stefanosn
    Oct 31, 2011 at 22:10
  • @stefanosn. Firstly, you don't need to save the button. you just need to make sure that you are recording the state of the button having to do with a piece of data. In your cellForRowAtIndexPath method, make sure you are setting whether the button is hidden or not outside the if statement that checks to see if the cell that has been reused is nil or not. Please post the code for the the cellForRowAtIndexPath method Oct 31, 2011 at 22:49
  • oh my god you were right timthetoolman! The button.hidden should have been outside the if (cell==nil) ! Now it is working but when i scroll down the button dissapears but i think that is how it should work! Maybe i can find a way to hide it when the scroll begins! Thank you so much! :)
    – stefanosn
    Oct 31, 2011 at 23:25
  • I have searched a little more about this matter some of the following code might help some people label2.backgroundColor=[UIColor colorWithPatternImage:image]; [label2 setNeedsDisplay]; setneeddisplay has to be called when you add an image with cgrectmake . And you have to use in your cellforrowatindexpath this code NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i-%i", indexPath.section, indexPath.row]; I have spent may hours on this hope it helps someone! Have faith!
    – stefanosn
    Nov 3, 2011 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.