iOS:如何在viewForHeaderInSection中向自定义视图添加“滑动到删除”按钮

iOS:如何在viewForHeaderInSection中向自定义视图添加“滑动到删除”按钮,ios,uitableview,view,uiswipegesturerecognizer,Ios,Uitableview,View,Uiswipegesturerecognizer,使用viewForHeaderInSection,有一个类似展开折叠的表格视图,具有自定义标题。 在这种情况下,我想添加滑动手势功能来删除相同的视图,即节头 我的viewForHeaderInSection代码是 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { mView = [[[UIView alloc]initWithFrame:CGRectMake

使用
viewForHeaderInSection
,有一个类似展开折叠的表格视图,具有自定义标题。 在这种情况下,我想添加滑动手势功能来删除相同的视图,即
节头

我的
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;
}
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分
{
mView=[[UIView alloc]initWithFrame:CGRectMake(0,0320110)]autorelease];
mView.backgroundColor=[UIColor-whiteColor];
[mView setBackgroundColor:[UIColor whiteColor]];
UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(10,1029028)];
title.text=[[updateDataArray objectAtIndex:section]objectForKey:@“message”];
title.font=[UIFont fontWithName:@“Raleway Medium”大小:18];
UIButton*bt=[UIButton按钮类型:UIButtonTypeCustom];
[bt设置帧:CGRectMake(0,0320,44)];
[bt SETTITLECLOR:[UIColor blueColor]用于状态:UIControlStateNormal];
[bt设置标签:部分];
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:)for ControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];

if(section这里有代码和。这里是制作自己的代码的方法。现在,如果您想在标题或任何其他视图中实现这一点,只需将tableview单元格更改为所需的视图。

您可以将
uisweegestureerecognizer
添加到每个
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");
    }

}

在返回视图之前,在ViewForHeader部分中添加以下行:

   - (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
       }
   }
这是实现您所需的基本想法,请根据您的项目要求更改此代码


希望这有帮助。

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

最后,为performance delete编写代码。

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