ios 在 UITableViewCell 的左侧打勾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8755506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Put checkmark in the left side of UITableViewCell
提问by Nicu Surdu
I want to make something similar to the WiFi settings page: when you tap the table cell, put a checkbox in the left side of the cell, and have a disclosure button accessory view on the right to show more details.
我想做一些类似于 WiFi 设置页面的东西:当你点击表格单元格时,在单元格的左侧放置一个复选框,并在右侧有一个显示按钮附件视图以显示更多详细信息。
My question: is there a way to put the checkmark in the left side of a UITableViewCell without building a custom UITableViewCell ?
我的问题:有没有办法在不构建自定义 UITableViewCell 的情况下将复选标记放在 UITableViewCell 的左侧?
回答by Eddie Gasparian
The answer is YES! You don't have to create a custom cell for that or add image views. To simulate checkboxes you only have to prepend a unicode symbol for a checkbox state to the cell text.
答案是肯定的!您不必为此创建自定义单元格或添加图像视图。要模拟复选框,您只需要将复选框状态的 unicode 符号添加到单元格文本中。
The unicode symbols I'm using for checkboxes are \u2705
for checked and \u2B1C
for unchecked (nearly the same as \U0001F533
on iOS 5). iOS renders several unicode symbols as icons.
我用于复选框的 unicode 符号\u2705
用于选中和\u2B1C
未选中(几乎与\U0001F533
iOS 5相同)。iOS 将几个unicode 符号呈现为图标。
Here are some other symbols:
以下是一些其他符号:
@"\u2611", @"\u2B1C", @"\u2705", @"\u26AB", @"\u26AA", @"\u2714", @"\U0001F44D", @"\U0001F44E"
Imitating the Wi-Fi settings page (with UITableViewCellStyleValue1
):
模仿Wi-Fi设置页面(带UITableViewCellStyleValue1
):
cell.textLabel.text = @"\u2001 Wi-Fi 1";
cell.detailTextLabel.text = @"\U0001F512 \u268A";
cell.textLabel.text = @"\u2001 Wi-Fi 2";
cell.detailTextLabel.text = @"\U0001F512 \u268C";
cell.textLabel.text = @"\u2713 Wi-Fi 3";
cell.detailTextLabel.text = @"\U0001F513 \u2630";
回答by JOM
Of course you can do that :) For example use UITableViewCellStyle
当然你可以这样做:) 例如使用 UITableViewCellStyle
UITableViewCellStyleDefault, // Simple cell with text label and optional image view //(behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleDefault, // Simple cell with text label and optional image view //(behavior of UITableViewCell in iPhoneOS 2.x)
...and put a custom "checkmark" image in that "optional image view".
...并在该“可选图像视图”中放置自定义“复选标记”图像。
回答by Azam
In Swift you can press Command + Ctrl + Spacebar to show emoticons and special characters or you can copy this ? ??
在 Swift 中,您可以按 Command + Ctrl + 空格键来显示表情符号和特殊字符,或者您可以复制这个 ? ??
回答by D. Rothschild
So here is a way to do it. Place a UIImageView on the left edge of the cell. Do not set an image so it's blank as a start.
所以这是一种方法。在单元格的左边缘放置一个 UIImageView。不要将图像设置为空白作为开始。
Then, assuming you are doing a custom cell and a class for that UITableViewCell, do something like this:
然后,假设您正在为该 UITableViewCell 执行自定义单元格和类,请执行以下操作:
import UIKit
class MyCustoTableViewCell: UITableViewCell {
@IBOutlet weak var commandOption: UILabel!
@IBOutlet weak var checkMarkImage: UIImageView!
// Sets a left side checkmark on a cell when selected
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.checkMarkImage.image = selected ? UIImage(named: "BlueCheck") : .None
self.commandOption.font = selected ? UIFont(name: "Avenir-Heavy", size: 22) : UIFont(name: "Avenir-Book", size: 22)
// more check mark on the right side
// self.accessoryType = selected ? .Checkmark : .None
}
}
Note the bottom commented out is if you want the checkmark on the right. The super.setSelecdted method fires when a the user has tapped on the table cell. Be sure you have this implemented also:
注意底部注释掉的是如果你想要右边的复选标记。super.setSelecdted 方法在用户点击表格单元格时触发。确保你也实现了这个:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// do any changes needed here or leave blank if just adding check
}
}
Finally, make sure you have @2x and @3x check mark PNGs assets in your Assets folder. Note those are called "BlueCheck" in this example but use what ever your check mark assets are called.
最后,确保您的 Assets 文件夹中有 @2x 和 @3x 复选标记 PNGs 资产。请注意,在此示例中这些称为“BlueCheck”,但使用您的复选标记资产的名称。
This approach makes sure that when a user selects a new row the check mark goes away on the current cell.
这种方法确保当用户选择新行时,当前单元格上的复选标记消失。
In this case I'm bolding the text that is to the right of the image view to further indicate it was the selected row.
在这种情况下,我将图像视图右侧的文本加粗,以进一步表明它是选定的行。
回答by vtechig
No. You have to create a custom cell for that.
不,您必须为此创建一个自定义单元格。
回答by Sylvain G.
You should look at the UITableViewCell reference and Table View Guide. Use standard cell style with editingAccessoryType
property set to UITableViewCellAccessoryCheckmark
.
您应该查看UITableViewCell 参考和 Table View Guide。使用editingAccessoryType
属性设置为 的标准单元格样式UITableViewCellAccessoryCheckmark
。