检测ios中滚动视图内按钮的触摸

时间:2013-12-05 11:33:19

标签: ios uitableview uiscrollview uibutton

我有一个带有自定义单元格的表视图。在单元格中有一个水平滚动的滚动视图。在表格视图单元格上添加了一个按钮。但按钮没有响应触摸。滚动中没有添加按钮在这个按钮上单击,我的任务是更改其后面的滚动视图的偏移量,就像下一个按钮一样。如何检测触摸?这是我添加内容以滚动视图的代码。

 for(int i=0;i<[exerciseArrayForeachSection count];i++){

        Exercise *data = [exerciseArrayForeachSection objectAtIndex:i];

        [_customCell.exTitleLabel setText:data.exName];
         if([data.exImage length] < 1){
             [[AsyncImageLoader sharedLoader] cancelLoadingURL:_customCell.exThumbImageView.imageURL];
         }
      else  if ([data.exImage length] > 0)
        {
            int intExerId = [data.exImage intValue];
            NSString *fullPathString = [NSString stringWithFormat:@"%@%d/%@",EXER_URL,intExerId,data.exImage];
            NSURL* aURL = [NSURL URLWithString:fullPathString];


            _customCell.exThumbImageView.imageURL = aURL;
            imageUrlDetails=fullPathString;
        }

     [_customCell.scroll addSubview:_customCell.excerciseDetailsView];


         _customCell.scroll.contentOffset=CGPointMake(_customCell.scroll.contentOffset.x+320, _customCell.scroll.contentOffset.y);

        }

    [_customCell addSubview:_customCell.scroll];
     [_customCell.scroll bringSubviewToFront:_customCell.nextBtn];


        }

从自定义单元格视图调用按钮单击的方法,如:

-(IBAction)nextClickActn:(id)sender{
   [[self delegate]nextButtonAction:self];}

在表视图控制器中,

- (void)  nextButtonAction:(id)sender {

    UITableViewCell *cell = (UITableViewCell *)sender;
    _customCell=(CustomCellFor_Dashboard*)cell;


   if ( _customCell.scroll.contentOffset.x <= _customCell.scroll.contentSize.width ) {
       CGRect frame;
       frame.origin.x = _customCell.scroll.contentOffset.x +_customCell.scroll.frame.size.width;
       frame.origin.y = 0;
       frame.size = _customCell.scroll.frame.size;
       [_customCell.scroll scrollRectToVisible:frame animated:YES];
      // pageControlBeingUsed = YES;
   }

}

2 个答案:

答案 0 :(得分:2)

试试这个, 将其实现为cellForRowAtIndexPath方法:

UIButton *accessoryView = [[UIButton alloc] initWithFrame:CGRectMake(190, 5, 40, 41)];

accessoryView.tag = indexPath.row;
             [accessoryView addTarget: self action:@selector(yourMethodWhichUWantToCall:)forControlEvents:UIControlEventTouchUpInside];
             cell.accessoryView = accessoryView;

然后在该方法中实现以下代码:

UIButton *accessoryView = (UIButton *)sender;

UITableViewCell *cell;

if(isIOS7)
{
   cell = (UITableViewCell *)accessoryView.superview.superview;
}
else
{
   cell = (UITableViewCell *)accessoryView.superview;
}

答案 1 :(得分:1)

我认为你需要使用这一行

self.button.userInteractionEnabled = YES;

或者你可以使用。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];

// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;    

[pageScrollView addGestureRecognizer:tapGesture];