私のアプリの特定の部分テーブル内の場所、必要な場合は複数の場所を選択し、それらの場所をラベルに印刷して、選択したものがきちんと表示されるようにしたい、彼らの前では。これを行うには、didSelectRowAt
indexPathに何を入れる必要がありますか?それはすべて同じViewController上にあります。複数の選択tableView
テーブルセルを選択すると、別のビューコントローラに移動したくありません。私は遠くダウンテーブルからある項目を選択するたびにEDIT
@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