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

时间:2014-12-22 07:10:28

标签: ios uitableview view uiswipegesturerecognizer

使用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;
}

请建议如何创建滑动以删除按钮功能,如表格视图行?我也希望在标题标题中使用该按钮。

4 个答案:

答案 0 :(得分:2)

在返回视图之前在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
       }
   }

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

希望这有帮助。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您可以将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");
    }

}

答案 3 :(得分:0)

将yourView和deleteButton添加到容器视图中,向youView添加滑动左手势。

textfield

该滑动手势功能。

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

最后,将代码写入性能删除。