我正在使用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上才可用?


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

最佳答案

截至目前,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
}

关于ios - 滑动 Action 不适用于节标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56249458/

10-15 14:08