3

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.

iOSNoob
  • 1,420
  • 2
  • 16
  • 30
  • You are not returning any view from viewForHeaderInSection method ? – Mrunal Dec 22 '14 at 07:24
  • Also, once header is deleted, do you require to delete entire section as well (all the rows inside it)? – Mrunal Dec 22 '14 at 07:25
  • Neither Swipe gesture is working not I can apply any table view delegate methods to it as it is a custom view. – iOSNoob Dec 22 '14 at 07:31

4 Answers4

2

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.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
1

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.

Saad
  • 8,857
  • 2
  • 41
  • 51
0

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

}
Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
0

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.

Leon Liu
  • 11
  • 2