2017-07-06 74 views
0

我的应用程序的某个部分我希望用户能够选择表格中的位置,如果他们想要多个位置,并且将这些位置打印到标签中,以便他们可以看到他们所选择的整齐和在他们面前。为了做到这一点,我需要将didSelectRowAt indexPath放到什么位置?它们都在同一个ViewController上。Multiple Selection tableView

我不想在选择表格单元格时转到另一个视图控制器。

@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)" 

回答

0
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; 

     } 
    } 
+0

谢谢。我正在使用这个大纲。 – Sarah

+0

答案应该更多地描述解决方案。添加一些评论。 – timiTao

0

斯威夫特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() 

}