iOSDevp iOSDevp - 7 days ago 6
Objective-C Question

hide the custom button in the uitableviewcell when long pressed in ios

i have put one custom button inside in the table cell.here i want to hide that button when long press the cell and shows when the cancel button is clicked.

this Before Editing:

enter image description here

the arrow button is custom button, this want to be hide when long presssed,show when cancel is clicked
enter image description here

here the table delegate functions:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [name count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"select press");

static NSString *identifier=@"Cell";
UITableViewCell *cellVal=[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (cellVal == nil) {
cellVal = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cellVal.layer.cornerRadius = 9;
titleLabel=(UILabel *)[cellVal viewWithTag:3];
locationLabel=(UILabel *)[cellVal viewWithTag:4];
timeLabel=(UILabel *)[cellVal viewWithTag:5];
detailButton=(UIButton *)[cellVal viewWithTag:10];
detailButton.tag=indexPath.row;

if(!btn){
detailButton.hidden=NO;
}
else{detailButton.hidden=YES;
}

titleLabel.text=[name objectAtIndex:indexPath.row];
locationLabel.text=[loc objectAtIndex:indexPath.row];
timeLabel.text=[time objectAtIndex:indexPath.row];

if (userSelectedAll == TRUE) {
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
} else if (userSelectedAll == FALSE) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
return cellVal;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didSelectRowAtIndexPath %ld",(long)indexPath.row);
[arSelectedRows addObject:indexPath];

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did De SelectRowAtIndexPath");
[arSelectedRows removeObject:indexPath];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
[[DataBaseFile getSharedInstance]delete:[[DataBaseFile getSharedInstance] getDBFilePath] withName:[[reminderList objectAtIndex:indexPath.row] integerValue]];
}
[tableView reloadData];
}


Below is the long press code:

-(void)handleLongPress1:(UILongPressGestureRecognizer *)gestureRecognizer
{
self.cancelOut.hidden=NO;
self.selectAllOut.hidden=NO;
btn=TRUE;
self.tableVW.allowsSelection=NO;
self.tableVW.allowsMultipleSelection=YES;
self.toolBarOut.hidden=NO;
self.tableVW.allowsMultipleSelectionDuringEditing = YES;
[self.tableVW setEditing: YES animated: YES];
}


Cancel Button Code:

-(void) cancelTableEditClick: (UIButton *) sender
{
self.cancelOut.hidden=YES;
self.selectAllOut.hidden=YES;
btn=FALSE;
self.tableVW.allowsSelection = NO;
self.toolBarOut.hidden=YES;
[self.tableVW setEditing: NO animated: YES];
[self.tableVW reloadData];
}

Answer

i got simple answer,just do like this,inside the cellForRowAtIndexPath: delegate function, set this custom button to the Accessoryview,

UIButton *myAccessoryButton = [[UIButton alloc] init];
[myAccessoryButton setFrame:CGRectMake(0, 0, 48.0, 48.0)];
[myAccessoryButton setImage:[UIImage imageNamed:@"arrow_button.png"] forState:UIControlStateNormal];
[myAccessoryButton addTarget:self action:@selector(detailBtn:) forControlEvents:UIControlEventTouchUpInside];
[cellVal setAccessoryView:myAccessoryButton];

then the cal the detail function,

- (IBAction)detailBtn:(UIButton*)sender {
    NSLog(@"Detail btn clicked.... ");
}

now you can hide the button when you long pressed