【问题标题】:Not displaying the title in Swipe actions for UItableView Swift 4未在 UItableView Swift 4 的滑动操作中显示标题
【发布时间】:2018-11-07 18:18:27
【问题描述】:

我在 UItableViewCell 的前端设置了“添加到购物车”的操作。我已经设置了背景颜色、图像和标题。下面是我的代码。

 @available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let addToCart = UIContextualAction(style: .normal, title: "Add to Cart") { (action, view, nil) in
        print("Added to cart")

    }
    addToCart.title = "Add to Cart"
    addToCart.backgroundColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
    addToCart.image = UIImage(named: "cart")

    return UISwipeActionsConfiguration(actions: [addToCart])
}

以前的尝试:我没有使用addToCart.title = "Add to Cart" 定义标题,但是在没有得到它之后,我已经设置了。

我添加的图片大小为 25*25,背景清晰,为 .png 格式。

【问题讨论】:

  • 你有什么解决办法吗?

标签: ios xcode uitableview swift3


【解决方案1】:

UIContextualAction 支持 textimage。通过用setImage:属性设置图片,基本上在创建对象的时候去掉标题。如果您需要文本和图像,则需要创建带有嵌入文本的图像

这是UIContextualAction 中的错误。它不会同时显示图像和标题除非表格视图单元格高度为 91 点。有关更多信息,您可以获取forums.apple

【讨论】:

    【解决方案2】:

    试试这个

    Add swipe to delete UITableViewCell

    @available(iOS 11.0, *)    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
                let action =  UIContextualAction(style: .normal, title: "", handler: { (action,view,completionHandler ) in
                    //do stuff
                    completionHandler(true)
                    let data:NSDictionary = self.conversations[indexPath.row] as! NSDictionary
                    print(data)
                    let alert:UIAlertController = UIAlertController(title: "", message: "are you sure want to delete ?", preferredStyle: .alert)
    
                    alert.addAction(UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: { (action) in
                    }))
                    self.present(alert, animated: true, completion: nil)
                })
                action.image = UIImage(named: "")
                action.backgroundColor = UIColor(red: 0/255, green: 148/255, blue: 204/255, alpha: 1.0)
                let confrigation = UISwipeActionsConfiguration(actions: [action])
    
                return confrigation
            } 
    

    【讨论】:

    • 如果我不想删除行怎么办?!
    • 提问者需要UIContextualAction supports both text and image的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多