我的应用程序的某个部分我希望用户能够选择一个表中的位置,如果他们想要多个,并有这些位置打印到标签,以便他们可以看到他们选择了整齐和前面。要执行此操作,我需要在didSelectRowAt indexPath中输入什么内容?它们都在同一个ViewController上。
当我选择一个表单元格时,我不想转到另一个视图控制器。
@IBOutlet weak var labelGifteeLocationsPreview: UILabel!
@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsLocations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellLocations: UITableViewCell = UITableViewCell()
cellLocations.textLabel?.text = itemsLocations[indexPath.row]
return cellLocations
}编辑
现在,每当我选择一个离表更低的项目时,都会带来索引超出范围的错误。我不知道为什么只有当我选择离表更远的项目时才会发生这种情况。代码如下:
@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]
@IBOutlet weak var labelGifteeLocationsPreview: UILabel!
var locationsPreviewtext = [String]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
locationsPreviewtext.append(itemsLocations[indexPath.row])
labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if locationsPreviewtext.isEmpty {
return
}
else {
let ind = index(ofAccessibilityElement: locationsPreviewtext[indexPath.row]) //this is where I get the index out of range error
if ind == NSNotFound {
locationsPreviewtext.remove(at: indexPath.row)
}
}
labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"发布于 2017-07-07 14:43:58
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){
let cell = tableLocations.cellForRowAtIndexPath(indexPath)
if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark){
cell!.accessoryType = UITableViewCellAccessoryType.None;
}else{
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;
}
}发布于 2017-11-26 17:14:59
Swift 4
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
if (cell?.accessoryType == UITableViewCellAccessoryType.checkmark){
cell!.accessoryType = UITableViewCellAccessoryType.none
}else{
cell!.accessoryType = UITableViewCellAccessoryType.checkmark
}
}另一种方式,我推荐它
var tags = [["id":1,"name":"html"],["id":2,"name":"php"],["id":3,"name":"javascript"],["id":4,"name":"python"]]
var selectedTags = [Int]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tags.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TagsTableViewCell", owner: self, options: nil)?.first as! TagsTableViewCell
if(selectedTags.contains(tags[indexPath.row]["id"].intValue)){
cell.accessoryType = .checkmark
}
else{
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if(selectedTags.contains(tags[indexPath.row]["id"].intValue)){
selectedTags = selectedTags.filter { $0 != tags[indexPath.row]["id"].intValue }
}
else{
selectedTags.append(tags[indexPath.row]["id"].intValue)
}
tagsTableView.reloadData()
}https://stackoverflow.com/questions/44959809
复制相似问题