ios - UITableViewRowAction 标题作为图像图标而不是文本

标签 ios uitableview swift

我想在 Swift 的 tableviewCell 的滑动 Action 中放置图标(图像)而不是文本。

但我不知道如何用图片代替标题文字?

我的代码如下:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {

    var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Share" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        // 2
        let shareMenu = UIAlertController(title: nil, message: "Share using", preferredStyle: .ActionSheet)

        let twitterAction = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

        shareMenu.addAction(twitterAction)
        shareMenu.addAction(cancelAction)


        self.presentViewController(shareMenu, animated: true, completion: nil)
    })
    // 3
    var rateAction =    (style: UITableViewRowActionStyle.Default, title: "rate",im , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        // 4

        let rateMenu = UIAlertController(title: nil, message: "Rate this App", preferredStyle: .ActionSheet)

        let appRateAction = UIAlertAction(title: "Rate", style: UIAlertActionStyle.Default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

        rateMenu.addAction(appRateAction)
        rateMenu.addAction(cancelAction)


        self.presentViewController(rateMenu, animated: true, completion: nil)
    })

    // 5
    return [shareAction,rateAction]
}

有人可以帮我解决这个问题吗?

提前致谢

最佳答案

有两种方法可以实现这一点。

  • 如果符合您的目的,请在您的文本中使用 Unicode 字符,但 Unicode 字符集有限。
  • 使用符合您目的的表情符号。

这就是你在代码中的做法

 override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .Default, title: "\u{267A}\n Delete") { action, index in
        print("more button tapped")
        self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath)
    }
    delete.backgroundColor = UIColor(rgba: "#ef3340")

    let apply = UITableViewRowAction(style: .Default, title: "\u{2606}\n Like") { action, index in
        print("favorite button tapped")
        self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.Insert, forRowAtIndexPath: indexPath)
    }
    apply.backgroundColor = UIColor.orangeColor()

    let take = UITableViewRowAction(style: .Normal, title: "\u{2605}\n Rate") { action, index in
        print("share button tapped")
        self.tableView(tableView, commitEditingStyle: UITableViewCellEditingStyle.None, forRowAtIndexPath: indexPath)
    }
    take.backgroundColor = UIColor(rgba: "#00ab84")

    return [take, apply, delete]
}

这是我可以通过上述方式实现的 -

enter image description here

关于ios - UITableViewRowAction 标题作为图像图标而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740884/

相关文章:

ios - Trigger.io 和 startMonitoringSignificantLocationChanges 可能吗?

ios - 协议(protocol)构造错误 Objective C

ios - Swift 中等效的 android.content.BroadcastReceiver

swift - 带有大标题 iOS 11 的导航栏图像

ios - 选中行时防止 TableView 滚动

ios - Mac 10.14、10.14 vm 的 xcode 无法在 azure 托管代理上启动模拟器以进行 native 构建

ios - 快速选择特定文本字段时移动 View

objective-c - 未调用 UIPickerViewDelegate 方法

ios - 增加 UITableViewCell 分隔符的高度

ios - UISplitViewController & UITableView滑动删除被SectionIndex遮挡