前の選択されたセルをその文字列で渡す方法。 APIから私は "|"セパレータを取り外した後、array
に戻しています。しかし結果をtableView
に渡すにはどうすればいいのでしょうか?APIからの結果としてSELECT表ビューセル
これは選択を行って結果を配列に保存する方法のダミーコードです。
class TableViewController: UITableViewController
{
let limit = 5
var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)
var genreList = ["A","B","C","D","E","F","G","H"]
var stringRepresentation = String()
var result = [String]()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel!.text = genreList[indexPath.row]
if cell.selected
{
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
else
{
cell.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if let sr = tableView.indexPathsForSelectedRows {
if sr.count == limit {
let alertController = UIAlertController(title: "Oops", message:
"You are limited to \(limit) selections", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in
}))
self.presentViewController(alertController, animated: true, completion: nil)
return nil
}
}
return indexPath
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
print("selected \(genreList[indexPath.row])")
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell!.selected == true
{
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
result.append((cell?.textLabel?.text)!)
print(result)
}
else
{
cell!.accessoryType = UITableViewCellAccessoryType.None
print(result)
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let deselectedCell = tableView.cellForRowAtIndexPath(indexPath)
result = result.filter { $0 != deselectedCell?.textLabel?.text }
stringRepresentation = result.joinWithSeparator("|") // "1-2-3"
print(stringRepresentation)
deselectedCell!.accessoryType = UITableViewCellAccessoryType.None
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return genreList.count
}
}
あなたのテーブルビューが複数選択モードを持っていますか? –
はい、5までです。コードを確認してください。 – Nitesh