滑动操作不适用于节标题

时间:2019-05-22 04:43:05

标签: ios xcode uitableview swift4.2

我正在使用swift 4.2和xcode 10.1。我正在一个项目上,并创建了一些漂亮的扩展/可折叠UITableView。

一切正常而且很棒。然后,需要引入一些滑动操作。所以创建它们看起来很容易,这就是我使用和创建它们的方式

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let deleteAction = UIContextualAction(style: .destructive, title: "Add") { (action, view, handler) in
        print("Add Action Tapped")
    }
    deleteAction.backgroundColor = .green
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
    return configuration
}

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
        print("Delete Action Tapped")
    }
    deleteAction.backgroundColor = .red
    let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
    return configuration
}

但这对我的部分无效。当我向左或向右滑动时,该行会出现可滑动的动作。但我的栏标题上没有。这是我为节标题创建视图的方式。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


    let cell = Bundle.main.loadNibNamed("HeaderCell", owner: self, options: nil)?.first as! HeaderCell

    let shoppingList = mDataSource[section]

    let itemCount = shoppingList.ShoppingItems.count

    let txtStoreName = shoppingList.Store.P_FriendlyName! + " (\(itemCount))"

    cell.lblName.text = txtStoreName

    let imageHollow  = UIImage(named: "checkbox_hollow") // For List Item Check box image

    cell.btnSelectHeader.setImage(imageHollow, for: .normal)

    if(shoppingList.IsSelected){
        let imageChecked  = UIImage(named: "checkbox_checked") // For List Item Check box image
        cell.btnSelectHeader.setImage(imageChecked, for: .normal)
    }



    if(shoppingList.IsExpanded){

        let image  = UIImage(named: "ic-indicator-up")

        cell.ivIndicatorExpandCollapse.image = image

        cell.viewContentView.backgroundColor = CommonUtils.hexStringToUIColor(hex: AppColors.colorItemSelector)


    }else{

        let image  = UIImage(named: "ic-indicator-down")

        cell.ivIndicatorExpandCollapse.image = image

        cell.viewContentView.backgroundColor = CommonUtils.hexStringToUIColor(hex: AppColors.colorWhite)
    }


    cell.backgroundView?.backgroundColor = UIColor.white

    cell.isCellSwipable = IS_CELL_RIGHT_SWIPEABLE
    cell.delegateHeader = self
    cell.clickedSection = IndexPath.init(row: 0, section: section)
    cell.clickedModel = shoppingList


    return cell

}

问题:这是我的问题和疑问

  • 部分标题未像其他单元格一样显示滑动动作。
  • 这些滑动操作是否可以从iOS 11.0到最新版本使用,或者仅在iOS 11上有效?

请帮助我,我不确定为什么它不能在节标题上使用????

1 个答案:

答案 0 :(得分:0)

截至目前,iOS不允许滑动部分标题。我想建议您使用自己的逻辑。您可以通过自己更改节标题的视图约束(默认视图或滑动视图)来进行管理。

   func sectionSwiped(gestureRecognizer: UITapGestureRecognizer) {

    if let viewSwiped = gestureRecognizer.view as? UIView{
        swipedSection = viewSwiped.tag
    }
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let vw = UIView()
    vw.tag = section
    //You can customize view
    var swipeRight = UISwipeGestureRecognizer(target: self, action: "sectionSwiped:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    vw.addGestureRecognizer(swipeRight)
    return vw
}