Hide buttons in cell by other button press uitableview - ios

Wondering how can I get all items of each cell in UITableView. My problem is that I want to hide 2 buttons in cell when third is selected.
When I press on button 1, button 2 and 3 must be hidden. What I tried to do for that (in cellForRowAtIndexPath) :
AVMMovieButton *settings = (AVMMovieButton *)[cell viewWithTag:228];
[settings addTarget:self action:#selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
settings.tag = indexPath.row;
AVMMovieButton *playButton = (AVMMovieButton *)[cell viewWithTag:134];
[playButton setStore:oneItem];
[playButton addTarget:self action:#selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
playButton.tag = indexPath.row;
AVMMovieButton *down = (AVMMovieButton *)[cell viewWithTag:282];
AVMMovieButton *del = (AVMMovieButton *)[cell viewWithTag:161];
[settings setSelected:!settings.isSelected];
if (settings.isSelected)
{
NSLog(#"SELECTED!");
down.hidden = YES;
del.hidden = YES;
downLabel.hidden = YES;
delLabel.hidden = YES;
// [self performSelector:#selector(playButtonShow) withObject:nil afterDelay:0.3];
playButton.hidden = NO;
}
else
{
NSLog(#"UNSELECTED!");
playButton.hidden = YES;
down.hidden = NO;
del.hidden = NO;
downLabel.hidden = NO;
delLabel.hidden = NO;
NSLog(#"play button %d",playButton.hidden);
}
and then I added method for selecting my "settings" button:
-(void)selectSettings:(AVMMovieButton *)sender
{
[sender setSelected:!sender.isSelected];
NSLog(#"you just select button");
}
but it doesn't work!
Actually NSLog(#"you just select button"); works, but buttons never hides.
What should I do to get my buttons and hide them?
SOLVED:
All I needed to do was to create custom UITableViewCell class and then access my cell as Jay Gajjar and Akhilrajtr said. After I just used my method for select/deselect my button.
What I've got:
-(void)selectSettings:(AVMMovieButton *)sender
{
AVMMovieButton *settings = (AVMMovieButton *)sender;
CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:readyTable];
NSIndexPath *indexPath = [readyTable indexPathForRowAtPoint:pointInTable];
AVMMovieCell *cell=(AVMMovieCell *)[readyTable cellForRowAtIndexPath:indexPath];
if (cell.settingsButton.isSelected)
{
NSLog(#"SELECTED!");
cell.downloadButton.hidden = YES;
cell.deleteButton.hidden = YES;
cell.downLabel.hidden = YES;
cell.delLabel.hidden = YES;
cell.playButton.hidden = NO;
}
else
{
NSLog(#"UNSELECTED!");
cell.playButton.hidden = YES;
cell.downloadButton.hidden = NO;
cell.deleteButton.hidden = NO;
cell.downLabel.hidden = NO;
cell.delLabel.hidden = NO;
NSLog(#"play button %d",cell.playButton.hidden);
}
[settings setSelected:!settings.isSelected];
}
Hope this can be helpful for somebody else!

Try this,
-(void)selectSettings:(AVMMovieButton *)sender{
[sender setSelected:!sender.isSelected];
CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
[_tableView beginUpdates];
[_tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[_tableView endUpdates];
NSLog(#"you just select button");
}

cellForRowAtIndexPath is called when Table get reloaded. In your case, what you have to do is:
- (void)button3Method:(id)sender
{
// Begin UITableView Update
[self.tableView beginUpdates];
NSIndexPath *indexPath = // Get cell indexpath for the selected row.
// Logic for getting the UITableViewCell on which you want hide two buttons.
// Logic for hiding those two buttons
// Below line performs reloading of particular UITableViewCell.
[self.tableView reloadRowsAtIndexPaths:#[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
// End UITaleView Update
[self.tableView endUpdates];
}
Let me know, if this works for you. Good luck.

In cellForRowAtIndexPath:
[cell. settings addTarget:self action:#selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
cell. settings.tag=600+indexPath.row;
In button IBAction:
-(void) selectSettings:(id) sender{
UIButton *btn=(UIButton *)sender;
cellMoreButtonIndex=btn.tag;
YOURCELL *cell=(YOURCELL *)[self.contentTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:btn.tag-600 inSection:0]];
[settings setSelected:!settings.isSelected];
if (settings.isSelected) {
NSLog(#"SELECTED!");
down.hidden = YES;
del.hidden = YES;
downLabel.hidden = YES;
delLabel.hidden = YES;
// [self performSelector:#selector(playButtonShow) withObject:nil afterDelay:0.3];
playButton.hidden = NO;
}else {
NSLog(#"UNSELECTED!");
playButton.hidden = YES;
down.hidden = NO;
del.hidden = NO;
downLabel.hidden = NO;
delLabel.hidden = NO;
NSLog(#"play button %d",playButton.hidden);
}
}

add a custom cell and Handle button click event and hide/unhide functionality on cell level

Related

UITextField in UITableView with InputView of DatePicker

I am attempting the following:
Within each cell I have date-from and date-to UITextField. When the user presses either TextField an InputView of the DatePicker appears with a Done button to complete.
My challenge is I only want the user to be able to select either of the TextFields inside the selected cell and no other. The user must press Done on InputView when they have finished, so the app can validate and reload TableView if necessary.
I have code that manages normal cell selection well when the 'keyboard' is shown, but cannot manage the UITextField behavior as I would like.
I share some code in the hope someone can help me:
- (void)keyboardWillShow:(NSNotification*)aNotification {
self.TableViewDiary.allowsSelection = NO;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
self.TableViewDiary.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ActivityDiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ActivityDiaryCellId"];
cell.activity = [self.activitycollection objectAtIndex:indexPath.row];
cell.LabelName.text = cell.activity.name;
cell.TextFieldStartDt.text = [self FormatPrettyTime :cell.activity.startdt];
cell.TextFieldEndDt.text = [self FormatPrettyTime :cell.activity.enddt];
cell.datePickerStart.date = cell.activity.startdt;
cell.datePickerEnd.date = cell.activity.enddt;
cell.tag = indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
ActivityDiaryCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.datePickerStart = [[UIDatePicker alloc] init];
self.datePickerStart.datePickerMode = UIDatePickerModeDateAndTime;
//self.datePickerStart.date = self.activity.startdt;
[self.datePickerStart addTarget:self action:#selector(datePickerStartValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value
self.datePickerEnd = [[UIDatePicker alloc] init];
self.datePickerEnd.datePickerMode = UIDatePickerModeDateAndTime;
//self.datePickerEnd.date = self.activity.enddt;
[self.datePickerEnd addTarget:self action:#selector(datePickerEndValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value
self.datePickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
[self.datePickerToolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(dismissPicker:)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.datePickerToolbar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
self.TextFieldStartDt.delegate = self;
self.TextFieldStartDt.inputView = self.datePickerStart;
self.TextFieldStartDt.inputAccessoryView = self.datePickerToolbar;
self.TextFieldEndDt.delegate = self;
self.TextFieldEndDt.inputView = self.datePickerEnd;
self.TextFieldEndDt.inputAccessoryView = self.datePickerToolbar;
}
Keyboard is showing because a user has tapped a text field, not because they selected a cell. That's why allowsSelection doesn't affect it.
Move the UITextFieldDelegate handling to your UITableViewController. Then you can easily decide whether to show a date picker or not:
if date picker is hidden, show it and remember a cell which textfield is on
if date picker is already visible, check whether text field belongs to the same cell and either update date picker or do nothing (by overriding textFieldShouldBeginEditing(_:) and returning false)
I moved the UITextFieldDelegate up to the TableViewController and included following code there:
-(bool) textFieldShouldBeginEditing:(UITextField *)textField {
if (self.TableViewDiary.allowsSelection) {
return true;
} else {
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
if ([cell.TextFieldStartDt isFirstResponder] || [cell.TextFieldEndDt isFirstResponder]) {
return true;
}
}
return false;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
}

How to get the collection cell in other function in ios?

I have UICollectionView on which contains so many custom cells.I have a long press gesture on it when user long press then cells start shaking & delete button is added on them.When i press the delete button then cell is removed from collection view.
Code for long press.
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
{
return;
}
if (indexPath == nil)
{
NSLog(#"couldn't find index path");
}
else
{
[[NSUserDefaults standardUserDefaults]setValue:#"yes" forKey:#"longPressed"];
[self.collection_view reloadData];
}
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(#"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
NSLog(#"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
}
pgr
= [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handePanPress:)];
// To detect after how many seconds you want shake the cells
pgr.delegate = self;
[self.collection_view addGestureRecognizer:pgr];
//show the done button here
navButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(navBtnDone:)];
self.navigationItem.rightBarButtonItem = navButtonDone;
}
when long gesture start i also add the right button on nav bar on press of nav bar button i stop animation & remove the delete button.I able to remove delete button in iPhone 5s but not in iPhone 6.
below is code for that
- (IBAction)navBtnDone:(id)sender
{
if([[[NSUserDefaults standardUserDefaults]valueForKey:#"longPressed"] isEqualToString:#"yes"])
{
[[NSUserDefaults standardUserDefaults]setValue:#"no" forKey:#"longPressed"];
[_deleteButton removeFromSuperview];
[self.collection_view reloadData];
[self.collection_view removeGestureRecognizer:pgr];
self.navigationItem.rightBarButtonItem=nil;
}
}
Here i have just added [_deleteButton removeFromSuperview]; how can i get cell in each function & remove the delete button.
To retrieve the cell based on its indexpath you can call
UICollectionViewCell *someCell = [myCollectionView cellForItemAtIndexPath:indexPath];
Then remove the delete button with
if (someCell) {
// Remove the delete button on the cell.
[someCell.deleteButton removeFromSuperView];
}
There are two ways.
To Store the IndexPath in a global variable.
In your .h
NSIndexPath *globalIndexPath
In your Gesture Recogniser
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
p = [gestureRecognizer locationInView:self.collection_view];
globalIndexPath = [self.collection_view indexPathForItemAtPoint:p];
//Other Stuff
}
In your Delete button action
- (IBAction)navBtnDone:(id)sender
{
//Other Stuff
UICollectionViewCell *cell = [myCollectionView cellForItemAtIndexPath: globalIndexPath];
if (cell) {
[cell.deleteButton removeFromSuperView];
}
}
Set the Indexpath.row value to the Barbutton's tag value
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
//Other Stuff
//show the done button here
navButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(navBtnDone:)];
navButton.tag = indexPath.row //IMPORTANT
self.navigationItem.rightBarButtonItem = navButtonDone;
}
In your Button Action
- (IBAction)navBtnDone:(id)sender
{
UIBarButtonItem *btn = (UIBarButtonItem *)sender;
UItableViewCell *cell = [myCollectionView cellForItemAtIndexPath: [NSIndexPath indexPathWithRow: btn.tag inSection:0]];
[cell.deleteButton removeFromSuperView];
}

Apple like shaking icons in UICollectionView in iOS? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I want to develop an app in which the items should be displayed as the UICollectionView. When user tap & hold the items all the items should start shaking like the Apple home screen on which icons start to shake when we want to delete an app. So please tell me how can I implement this feature. Is there any library can use?
Declare your variables first:
UIButton* _deleteButton;
CGPoint p; // It is a point which will give you which cell has been selected.
Add UIGestureRecognizerDelegate.
In .m file, in viewDidLoad, add UILongPressGestureRecognizer and UITapGestureRecognizer to your collectionView, since you want to shake the cells at long press:
- (void)viewDidLoad {
[super viewDidLoad];
// Add gesture recognizer to your collection view cell
UILongPressGestureRecognizer *lpgr
= [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = .3; // To detect after how many seconds you want shake the cells
lpgr.delegate = self;
[self.collectionView addGestureRecognizer:lpgr];
lpgr.delaysTouchesBegan = YES;
/// This will be helpful to restore the animation when clicked outside the cell
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
//lpgr.minimumPressDuration = .3; //seconds
tap.delegate = self;
[self.collectionView addGestureRecognizer:tap];
}
You can now implement handleLongPress: in your .m file. When you will long press your collectionViewCell, you will get (x,y) co-ordinates of where the user has pressed the cell, which we will store in point p.
Based on that point, you will be able to fetch the appropriate indexPath of the appropriate cell.
p = [gestureRecognizer locationInView:self.collectionView]; // Store (x,y) co-ordinate where the user has tapped the cell in point p.
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
Now, give animation to the cell, which has been tapped using CABasicAnimation of QuartzCore framework. At the time of animation, bring your _deleteButton up so that it is visible.
With handleTap:, you will then be able to restore animation when clicked outside of the collectionViewCell.
-(void)handleTap:(UITapGestureRecognizer *)gestureRecognizer
{
NSLog(#"singleTap");
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
p = [gestureRecognizer locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(#"couldn't find index path");
[[NSUserDefaults standardUserDefaults]setValue:#"no" forKey:#"longPressed"];
[[NSUserDefaults standardUserDefaults]setValue:#"yes" forKey:#"singleTap"];
//_deleteButton = [[UIButton alloc] initWithFrame:CGRectZero];
//[cell addSubview:_deleteButton];
//[_deleteButton removeFromSuperview];
[self.collectionView reloadData];
} else {
}
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
p = [gestureRecognizer locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(#"couldn't find index path");
} else {
[[NSUserDefaults standardUserDefaults]setValue:#"yes" forKey:#"longPressed"];
[self.collectionView reloadData];
}
}
Based on the selected item, delete the appropriate item.
-(void)deleteyourItem
{
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
//delete your item based on the `indexpath` from your collectionViewArray here.
//OR If you are accessing the database to display the collectionView, you can compare the value fetched based on the `indexPath`, with your database value and then delete it.
// Reload your collectionView after deletion
}
After you reload the collection view, cellForItemAtIndexPath:, will look like this:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"favoritecell" forIndexPath:indexPath];
// UIImageView *img=[[UIImageView alloc]init];
cell.backgroundColor = [UIColor colorWithRed:251.0/255.0 green:144.0/255.0 blue:13.0/255.0 alpha:1.0];
//img.image = [UIImage imageNamed:#""];
NSLog(#"%d",indexPath.row);
if([[[NSUserDefaults standardUserDefaults]valueForKey:#"longPressed"] isEqualToString:#"yes"])
{
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
[anim setToValue:[NSNumber numberWithFloat:0.0f]];
[anim setFromValue:[NSNumber numberWithDouble:M_PI/64]];
[anim setDuration:0.1];
[anim setRepeatCount:NSUIntegerMax];
[anim setAutoreverses:YES];
cell.layer.shouldRasterize = YES;
[cell.layer addAnimation:anim forKey:#"SpringboardShake"];
CGFloat delButtonSize = 75;
_deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, delButtonSize, delButtonSize)];
_deleteButton.center = CGPointMake(0, 0);
_deleteButton.backgroundColor = [UIColor clearColor];
[_deleteButton setImage: [UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[cell addSubview:_deleteButton];
[_deleteButton addTarget:self action:#selector(deleteRecipe) forControlEvents:UIControlEventTouchUpInside];
}
else if ([[[NSUserDefaults standardUserDefaults]valueForKey:#"singleTap"] isEqualToString:#"yes"])
{
for(UIView *subview in [cell subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
[cell.layer removeAllAnimations];
// _deleteButton.hidden = YES;
// [_deleteButton removeFromSuperview];
}
//251, 144 , 13
return cell;
}

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.

Duplicated cell when scrolling. What should I do in order to prevent it?

I'm a newbie with iOS programming language. I try to use table with button in each row.
when i click button in the first cell, it work pretty well.
but when i scroll down, the cell that i don't click also appear click.
I want to show the clicked button in only cell(s) that I clicked?
Code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
ClassifyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if ([[[arrayResult objectAtIndex:indexPath.row] objectForKey:#"COLABO_FLD_SRNO"] isEqualToString:#"I"]) {
cell.editButton.hidden = YES;
cell.folder_name.text = [[arrayResult objectAtIndex:indexPath.row] objectForKey:#"COLABO_FLD_NM"];
}else{
cell.editButton.hidden = NO;
cell.folder_name.text = [[arrayResult objectAtIndex:indexPath.row] objectForKey:#"COLABO_FLD_NM"];
cell.editButton.tag = indexPath.row;
[cell.editButton addTarget:self action:#selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.checkButton.tag = indexPath.row;
[cell.checkButton addTarget:self action:#selector(checkButtonActoin:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)checkButtonActoin:(UIButton *)sender{
if (sender.selected == YES) {
sender.selected = NO;
}else if(sender.selected == NO){
sender.selected = YES;
}else {
sender.selected = NO;
}
}
UITableView reuse the state of cell's subviews for new cells. To prevent reusing just configure your subviews explisitly in tableView:cellForRowAtIndexPath:. In your case you need to set selected property of your button to YES, if it was selected, and to NO, if it was not selected.
Add NSMutableArray property to your view controller:
#property (strong, nonatomic) NSMutableArray *arrayForSelectedIndexPaths;
Change your tap method:
-(void)checkButtonAction:(UIButton *)sender{
UITableViewCell *cell = (UITableViewCell *) sender.superview.superview...; //find appropriate number of superviews to get your cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (sender.selected) {
sender.selected = NO;
[self.arrayForSelectedIndexPaths removeObject:indexPath];
}else {
sender.selected = YES;
[self.arrayForSelectedIndexPaths addObject:indexPath];
}
}
Then in tableView:cellForRowAtIndexPath: add this line:
cell.checkButton.selected = [self.arrayForSelectedIndexPaths containsObject:indexPath];

Resources