2016-08-04 1 views
2

前の選択されたセルをその文字列で渡す方法。 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 
} 

} 
+0

あなたのテーブルビューが複数選択モードを持っていますか? –

+0

はい、5までです。コードを確認してください。 – Nitesh

答えて

3

今、この

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    if (self.result.count != limit) { 
     if (self.result.contains(genreList[indexPath.row])) { 
      let index = self.result.indexOf(genreList[indexPath.row]) 
      self.result.removeAtIndex(index) 
     } 
     else { 
      self.result.append(genreList[indexPath.row]) 
     } 
    } 
    else { 
     if (self.result.contains(genreList[indexPath.row])) { 
      let index = self.result.indexOf(genreList[indexPath.row]) 
      self.result.removeAtIndex(index) 
     } 
     else { 
      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) 
     } 
    } 
    stringRepresentation = result.joinWithSeparator("|") 
    self.tableView.reloadData() 
} 

ノートのようなdidSelectRowAtIndexPathを変更すること

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 result.contains(genreList[indexPath.row]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryType.None 
    } 
    return cell 
}  

のためにあなたのresult配列を使用し、この方法のようにセルを選択してください:ViewControllerからwillSelectRowAtIndexPath方法を削除その方法は必要ありません。

+0

didSelectRowAtIndexPathに - self.result.append(sender.tag)について説明してください。その与えるエラー。 – Nitesh

+0

編集を確認してください:) –

+0

ありがとうございます。しかし、5つのセルを選択した後の1つの問題(これは限界です)。その後、以前に選択したセルの選択を解除すると、5のAlertが表示されます。そのセルは既に選択されているので、選択解除されません。私は選択解除が必要だと思う。 – Nitesh

0

1つのモデルクラスを作成し、APIからの値または配列を取得し、このコードを実装

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") 

     let relModel = bussArray.objectAtIndex(indexPath.row) as! BussinesModel 

     cell?.textLabel?.text = relModel.businesses_name 
     var imageView : UIImageView 
     imageView = UIImageView(frame:CGRectMake(20, 20, 15, 15)) 

     if relModel.check 
     { 
      imageView.image = UIImage(named:"checked_checkbox") 
     } else{ 
      imageView.image = UIImage(named:"checkbox_unchecked") 
     } 
     cell!.backgroundColor = UIColor.whiteColor(); 
     cell!.tintColor = UIColor.grayColor() 
     cell!.accessoryView = imageView 

     return cell! 
    } 
関連する問題