iOS:如何将滑动删除按钮添加到 viewForHeaderInSection 中的自定义 View

标签 ios uitableview view uiswipegesturerecognizer

使用 viewForHeaderInSection 有一个类似展开-折叠的表格 View ,带有自定义标题。 在那,我想添加滑动手势功能来删除相同的 View ,即 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;
}

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

最佳答案

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

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

希望这对您有所帮助。

关于iOS:如何将滑动删除按钮添加到 viewForHeaderInSection 中的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27598066/

相关文章:

java - playFramework 中 Scala View 模板的转换和实例

iOS:当 View 位于弹出框后面时,我如何注意到?

objective-c - 如何实现UITableView-->Detail with Xcode4 Storyboards

ios - 跨设备建立同步音乐流

ios - 自定义 UITableViewCell 不显示重新排序控件

ios - 在 UITableViewCell 中水平居中放置两个 UILabel

android - 如何从 Android 中的 Activity 访问自定义适配器内 fragment 的 UI 元素

iphone - 检查 View 是否在运行时加载

ios - 在 IOS 上使用 CollectionView 的弹出窗口

ios - TableView :cellForRowAtIndexPath called with nil indexPath after deleting item