【问题标题】:iOS: How to add swipe to delete button to a custom view in viewForHeaderInSectioniOS:如何将滑动删除按钮添加到 viewForHeaderInSection 中的自定义视图
【发布时间】:2015-02-20 06:42:24
【问题描述】:

有一个使用viewForHeaderInSection 自定义标题的类似展开折叠的表格视图。 在那,我想添加滑动手势功能来删除相同的视图,即section header

我的viewForHeaderInSection 代码是,

- (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;
}

请建议如何创建滑动以删除表格视图行等按钮功能?我也想要节标题中的那个按钮。

【问题讨论】:

  • 您没有从 viewForHeaderInSection 方法返回任何视图?
  • 另外,一旦标题被删除,你是否也需要删除整个部分(其中的所有行)?
  • 滑动手势都不起作用,因为它是自定义视图,所以我可以对其应用任何表格视图委托方法。

标签: ios uitableview view uiswipegesturerecognizer


【解决方案1】:

在返回视图之前在 viewForHeaderInSection 中添加此行:

   - (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
       }
   }

这是实现您所需的基本想法,请根据您的项目要求更改此代码。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这里是代码DMSLidingCellTISwipeableTableView。这是制作自己的方法的方法。 make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views。现在,如果您想将其实现到您的标题或任何其他视图。只需将 tableview 单元格更改为您想要的视图。

    【讨论】:

      【解决方案3】:

      您可以将UISwipeGestureRecognizer 添加到每个HeaderView

      尝试以下方式

      - (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");
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        将 yourView 和 deleteButton 添加到容器视图,将向左滑动手势添加到 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;
        }
        

        这是滑动手势功能。

        -(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;}];
        }
        }
        

        最后,写代码去性能删除。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-02
          • 2014-11-15
          • 1970-01-01
          相关资源
          最近更新 更多