the event touchUpInside of a UIButton don't work - ios

there is only 1 cell in my UITableView, and I add a UIButton to the cell.
set the action when touchUpInside event happens. But it doesn't work at all. Meanwhile, if
i set the trigger as touchDown event instead of touchUpInside, it will fire the action i set.
I search the reason by google, someone says that the problem maybe caused by the superview's dimension. That is the size of the button is beyond its superview.
so I add a UIView to contain the button, the code is as follow:
self.ageDisplay = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage *ageImage = [UIImage imageNamed:#"long_btn_click.png"];
[self.ageDisplay setImage:ageImage forState:UIControlStateSelected];
[self.ageDisplay setFrame:CGRectMake(10, 5, 145, 34)];//(10, 320, 145, 25)];
[self.ageDisplay setTitle:#"请选择您的年龄" forState:UIControlStateNormal];
[self.ageDisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.ageDisplay setTag:3];
[self.ageDisplay addTarget:self action:#selector(showSelectionPage:) forControlEvents:UIControlEventTouchDown];
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, 320, 106)];
//self.buttonsSubView.backgroundColor = [UIColor blueColor];
[self.buttonsSubView setUserInteractionEnabled:YES];
[self.buttonsSubView addSubview:self.ageDisplay];
the above code is in ViewDidLoad function.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sugPageCell = #"SuggestionPageCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell];
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sugPageCell];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:self.sugTitle];
[cell addSubview:self.sugSpecification];
[cell addSubview:self.suggestion];
self.buttonsSubView.userInteractionEnabled = YES;
[cell addSubview:self.buttonsSubView];
/*
[cell addSubview:self.ageDisplay];
[cell addSubview:self.genderDisplay];
[cell addSubview:self.submit];
*/
return cell;
}
However, this doesn't help.
The problem has confused me for 2 days.
I will appreciate if anyone can provide some indication.
Thank you.

It's because your buttonSubView view is outside the cell's frame.
So even if the button is visible (because the cell's clipsToBounds is set to NO), it will not receive touch events from the cell. Why do you set this view's vertical position to 310 ?
If you try this :
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)];
The event should fire as you want.

I had this problem recently. I guess there must may be an object of UITapGesture added to your UITableView. If so, please set the property cancelsTouchesInView of the UITapGesture object to NO.
For example:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction];
gesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:gesture];

Related

UITableViewCell accessoryView overlap custom button of cell OR how to change accessoryView position?

I am using MSCellAccessory to set custom colour to accessoryView. It worked fine. But when I place button in cell and click on cell, checkmark is visible but it overlaps delete button. Check my snap shot.
So how to change cell accessoryView position in UITableViewCell or set accessoryView below delete button.
If you are sub-classing the UITableViewCell you can adjust it in layoutSubviews
- (void)layoutSubviews {
[super layoutSubviews];
CGRect accessoryViewFrame = self.accessoryView.frame;
accessoryViewFrame.origin.x = CGRectGetWidth(self.bounds) - CGRectGetWidth(accessoryViewFrame);
self.accessoryView.frame = accessoryViewFrame;
}
OTHERWISE......
Add a subview instead of accessoryView
UIButton *indicatorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
indicatorBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[indicatorBtn setBackgroundImage:[UIImage imageNamed:#"right_indicator.png"]
forState:UIControlStateNormal];
indicatorBtn.backgroundColor = [UIColor clearColor];
//indicatorBtn.alpha = 0.5;
indicatorBtn.tag = indexPath.row;
[indicatorBtn addTarget:self action:#selector(method:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:indicatorBtn];
I solved by adding UIImageView for tick. And put beside the delete button, and it works for me. I am not using table cell accessory. Please check below code.
- (UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier
{
CGRect cellRectangle = CGRectMake(0, 0, CGRectGetWidth(tableViewSavedSearch.frame), tableViewSavedSearch.rowHeight);
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.frame = cellRectangle;
CGFloat imageWidth = 30;
//Name
UILabel *lblName = [DesignModel createPaddingLabel:CGRectMake(0, 0, CGRectGetWidth(cellRectangle) - (imageWidth * 2), tableViewSavedSearch.rowHeight) labelTag:TAG_SAVED_SEARCH_NAME textColor:COLOUR_BLUE_DARK textAlignment:NSTextAlignmentLeft textFont:[UIFont fontWithName:FONT_CENTURY_GOTHIC size:14] padding:UIEdgeInsetsMake(0, 5, 0, 5)];
[cell.contentView addSubview:lblName];
//Delete button
UIButton *button = [DesignModel createImageButton:CGRectMake(CGRectGetMaxX(lblName.frame), 0, imageWidth, tableViewSavedSearch.rowHeight) image:imageDelete tag:TAG_SAVED_SEARCH_DALETE];
[button addTarget:self action:#selector(btnSaveAndSearch_DeleteAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
//TICK
UIImageView *imageView = [DesignModel createImageView:CGRectMake(CGRectGetMaxX(button.frame), 0, imageWidth, tableViewSavedSearch.rowHeight) image:imageTick tag:TAG_SAVED_SEARCH_TICK contentMode:UIViewContentModeCenter];
[cell.contentView addSubview:imageView];
return cell;
}

UICollectionviewCell image change on selction

I have UICollectionview controller with UIButton in each cell. On selecting the the button I have to change the button bg image, on double-tap I have to again change it. (Single tap to like, double-tap to love concept on user interest page).
What is the best way to do this?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [interestsCollection dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIButton* img=[[UIButton alloc]init];
[img setImage:[UIImage imageNamed:[imgTitleArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[img addTarget: self action: #selector(interestClicked:) forControlEvents: UIControlEventTouchUpInside] ;
img.frame=CGRectMake(10, 10, 60, 60);
[cell.contentView addSubview:img];
UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 80, 20)];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text=[titleArray objectAtIndex:indexPath.row];
lbl.font = [UIFont fontWithName:#"Arial" size:11];
lbl.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
[cell.contentView addSubview:lbl];
return cell;
}
You can handle such situation with single and double TapGesture. with help of respected selector, you can change UIImage of UIButton.
Here are the link that guide you. how to create double Tap Gesture on UICollectionView. with help of this you can created single Tap gesture as well.
Collection View + Double Tap Gesture
Add UITapGestureRecognizer to your button and change color as you want.
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTwice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
[self.button addGestureRecognizer:tapTwice];

iOS: How to add swipe to delete button to a custom view in viewForHeaderInSection

There is a expand-collapse like table view with custom header using viewForHeaderInSection.
In that, I want to add swipe gesture functionality to delete the same view i.e section header.
My code for viewForHeaderInSection is,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
mView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)]autorelease];
mView.backgroundColor=[UIColor whiteColor];
[mView setBackgroundColor:[UIColor whiteColor]];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 28)];
title.text=[[updateDataArray objectAtIndex:section] objectForKey:#"message"];
title.font = [UIFont fontWithName:#"Raleway-Medium" size:18];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 320, 44)];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt setTag:section];
addCellFlag=2;
[bt.titleLabel setFont:[UIFont systemFontOfSize:20]];
[bt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[bt.titleLabel setTextColor:[UIColor blueColor]];
[bt setBackgroundColor:[UIColor whiteColor]];
[bt addTarget:self action:#selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];
if (section<updateDataArray.count-1) {
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, 320, 0)];
lineView.backgroundColor=[UIColor lightGrayColor];
[bt addSubview:lineView];
[lineView release];
}
mView.backgroundColor=[UIColor clearColor];
[mView addSubview:title];
return mView;
}
Please suggest how to create a swipe to delete button functionality like table view row? I want that button in section header as well.
Add this lines in viewForHeaderInSection before returning your view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
....
// Your code here
....
mView.tag = section;
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(headerViewSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[mView addGestureRecognizer:sgr];
return mView;
}
- (void)headerViewSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gestureRecognizer.view;
// use mView.tag property to get the section index which you require to delete from array
// update your sectionDataSource array remove all the objects from section array
// so that it will reflect to numberOfSection method
// then remove all the rows which that section containing from your rowDataSource array
// so that it will reflect to numberOfRowsInSection method
// now call [tableView reloadData] method
}
}
This is a base idea to achieve what you required, change this code as per your project requirements.
Hope this helps.
Here are codes DMSLidingCell and TISwipeableTableView. And here is way how to make your own one. make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views. Now if you want to implement this to your header or any other view. Just change the tableview cell to your desired view.
you can add UISwipeGestureRecognizer to every HeaderView
Try with following way
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * viewTemp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[viewTemp setBackgroundColor:[UIColor redColor]];
[viewTemp setTag:(section + 100)];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen:)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft );
[viewTemp addGestureRecognizer:swipeGesture];
return viewTemp;
}
- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
UIView * viewTemp = (UIView *) [self.tableView viewWithTag:[gesture.view tag]];
if(viewTemp){
NSLog(#"Do your stuff");
}
}
Add yourView and deleteButton to a container view, add swipe left gesture to the yourView.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, thisHeaderHeight)];
//your view
YourView* view;
view.frame = CGRectMake(0, 0, self.view.frame.size.width, thisHeaderHeight);
//....your code
//add button
UIButton* _customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_customButton setTitle:NSLocalizedString(#"Delete", nil) forState:UIControlStateNormal];
_customButton.frame = CGRectMake(self.view.frame.size.width - thisButtonWidth, 0, thisButtonWidth, thisHeaderHeight);
[_customButton addTarget:(id)self action:#selector(onPressDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
//add swipe gesture
UISwipeGestureRecognizer* slgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[slgr setDirection:UISwipeGestureRecognizerDirectionLeft]; // change direction accordingly
[view addGestureRecognizer:slgr];
UISwipeGestureRecognizer* srgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[srgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[view addGestureRecognizer:srgr];
//view covers customButton
[mainView addSubview:customButton];
[mainView addSubview:view];
return mainView;
}
That's swipe gesture function.
-(void)onSwipeHeader:(UISwipeGestureRecognizer *)gr{
if (gr.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gr.view;
CGRect newFrame = mView.frame;
if(gr.direction == UISwipeGestureRecognizerDirectionLeft){
newFrame.origin.x = -thisButtonWidth;
}else if(gr.direction == UISwipeGestureRecognizerDirectionRight){
newFrame.origin.x = 0;
}
[UIView animateWithDuration:0.25 animations:^{mView.frame = newFrame;}];
}
}
Finally, write code to performance delete.

UITableView modify only one header section after delegate methods

I'm creating a UITableView with more section "closed". When I tap on a section, with a workaround, I make the section explode showing the rows for that section. The problem that I have is that I have to animate the header to collapse it to an half of its width.
This is the code I'm using
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
Sport *sport = [self.sportList objectAtIndex:section];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.frame.size.width, SECTION_HEIGHT)];
headerView.backgroundColor = [UIColor colorWithRed:31.0/255.0 green:59.0/255.0 blue:143.0/255.0 alpha:1.0];
UILabel *sportTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 2.0, tableView.frame.size.width, SECTION_HEIGHT-4)];
sportTitle.textColor = [UIColor whiteColor];
[headerView addSubview:sportTitle];
UIButton *sportButton = [[UIButton alloc] initWithFrame:headerView.frame];
[sportButton addTarget:self action:#selector(didSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[sportButton setTitle:#"" forState:UIControlStateNormal];
[sportButton setBackgroundColor:[UIColor clearColor]];
sportButton.tag = section;
[headerView addSubview:sportButton];
return headerView;
}
This is the selector:
-(void)didSelectedButton:(UIButton *)_button
{
if (self.delegate && [self.delegate respondsToSelector:#selector(didSelectedSport:)])
{
[self.delegate didSelectedSport:sport];
}
}
The delegate updates only the dataSource and calls the reloadData, making possible the explosion/collapse of the sections. But now I want to animate the changes to the width of the header for that section, is it possible? Thanks
EDIT: I can modify the header using the property superview of the UIButton in the selector, but I have to do it after the controller drew it
Try using the function below. Set your superview of uibutton as the from view in the method.
Create another view with your needed width and set it as to view in the method. Give a animation of choice in options.
[UIView transitionFromView:from
toView:to
duration:.5
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:^(BOOL finished) {
}];

How to set the position of ImageView after getting x and y postion on finger touch in iphone

I am making an ipad app in which I have tableView with custom it works fine but I want that when user clicks on add button in custom cell then it should get the position x and y of that button and then set the imageView to the center of that button.
I have searced code it gets points but how to add make this for cell.addButton as cell is custom.
[cell.imageButton addTarget:self action:#selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
Here is the code for touch
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
But in this way it does not call the method which is on cell.addButton imageButtonAction.
As I understand from you , you need to set an image in a UIButton upon tap.
If you added the UITapGestureRecognizer you removed the default tap recognition and now only this would work:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
You should remove the above code and only set this:
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
where the imageButtonAction: is :
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
}
If you want to add an image next to the button let's say in the left of the button then the function should look like this:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:#"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:#"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:#selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:#"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
now i am adding button to imageView but it does not get clicked
For UIButton you can use directly button function
-(IBAction) imageButtonAction:(id) sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
UIButton *btn = (UIButton *)sender;
//[btn setimage:[UIImage imagenamed:dimagename];
UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
[cell addSubview:backimage];
}

Resources