我的应用程序的某个部分我希望用户能够选择表格中的位置,如果他们想要多个位置,并且将这些位置打印到标签中,以便他们可以看到他们所选择的整齐和在他们面前。为了做到这一点,我需要将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)"
谢谢。我正在使用这个大纲。 – Sarah
答案应该更多地描述解决方案。添加一些评论。 – timiTao