xcode 在UItableView Swift 4的Swipe操作中不显示标题

jdzmm42g  于 2023-03-31  发布在  Swift
关注(0)|答案(3)|浏览(178)

我已经在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"定义title,但是在没有得到它之后,我已经设置了它。
我添加的图像大小为25*25,背景清晰,是.png格式。

gev0vcfq

gev0vcfq1#

UIContextualAction支持textimage,通过setImage设置图像:属性,创建对象时基本上去掉了标题,如果既要文本又要图片,则需要创建带嵌入文本的图片

这是UIContextualAction中的错误。它不能同时显示图像和标题**除非表格视图单元格高度为91点。**更多信息请参阅forums.apple

xuo3flqw

xuo3flqw2#

对于任何仍在寻找解决方案的人,我在Medium上找到了这个:https://forzfilm.medium.com/how-to-set-image-and-label-in-trailing-swipe-action-with-custom-font-ios11-4a49d4794669。事实证明,您可以将UIView渲染为Image。
注意:下面的答案是使用UICollectionLayoutListConfiguration,但你也可以使用它与tableView,因为他们都使用UIContextualAction。和单元格高度的例子是80。

使用UICollectionLayoutListConfiguration在XCode 14中测试:
SwipeLayout:

private func swipeLayout(icon: String, text: String, size: CGFloat) -> UIImage {
        let config = UIImage.SymbolConfiguration(pointSize: size, weight: .regular, scale: .large)
        let img = UIImage(systemName: icon, withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysOriginal)

        let label = UILabel(frame: .zero)
        label.font = .systemFont(ofSize: 13, weight: .medium)
        label.textColor = .white
        label.text = text

        let tempView = UIStackView(frame: .init(x: 0, y: 0, width: 50, height: 50))
        let imageView = UIImageView(frame: .init(x: 0, y: 0, width: img!.size.width, height: img!.size.height))
        imageView.contentMode = .scaleAspectFit
        tempView.axis = .vertical
        tempView.alignment = .center
        tempView.spacing = 2
        imageView.image = img
        tempView.addArrangedSubview(imageView)
        tempView.addArrangedSubview(label)

        let renderer = UIGraphicsImageRenderer(bounds: tempView.bounds)
        let image = renderer.image { rendererContext in
            tempView.layer.render(in: rendererContext.cgContext)
        }
        return image
    }

trailingSwipeActionsConfigurationProvider中:

// archive
let archiveHandler: UIContextualAction.Handler = { [weak self] action, view, completion in
     completion(true)
//   self?.updateSnapShot(animated: true)
}
let archiveAction = UIContextualAction(style: .normal, title: nil, handler: archiveHandler)
archiveAction.image = self.swipeLayout(icon: "archivebox.fill", text: "Archive", size: 16)
archiveAction.backgroundColor = .theme.arhiveRow

6bc51xsx

6bc51xsx3#

"试试这个"
添加滑动以删除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
        }

相关问题