2016-12-20 3 views
0

私は2つのセルを含むtableViewを持っています。最初のセルにはラベルが含まれ、2番目のセルにはDatePickerが含まれます。最初のセルは、クラスを使用しています。TableViewはReloadData()で更新されません

import UIKit 

class cell1: UITableViewCell { 
    @IBOutlet weak var label1: UILabel! 
} 

別のセルには、クラスを使用しています:あなたがDatePickerActionで見たよう

import UIKit 

class cell2: UITableViewCell { 
    @IBOutlet weak var outletPicker: UIDatePicker! 

    @IBAction func actionPicker(sender: AnyObject) { 
     let storyBoard = UIStoryboard(name : "Main" , bundle: nil) 
     let view = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 
     view.lblString = String(outletPicker.date) 
     view.tableView.reloadData() 
     view.tableView.layoutIfNeeded() 
    } 
} 

は、私が最初のセルのラベルを変更したいです。 mainClass( "TableViewControllerクラス")では、lblStringという名前の文字列を定義しました。それを変えてください。 MainClassのコードは以下の通りです。

import UIKit 

class ViewController: UITableViewController { 
    var lblString : String = "initialString" 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 2 
    } 

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 30 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     if(indexPath.section == 0) 
     { 
      if(indexPath.row == 0) 
      { 
      let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! cell1 
      cell.label1.text = lblString; 
      return cell 
      } 
     } 
     else 
     { 
      if(indexPath.row == 0) 
      { 
       let cell = tableView.dequeueReusableCellWithIdentifier("cell2") as! cell2 
       return cell 
      } 
     } 
     return UITableViewCell() 
    } 

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     if(indexPath.section == 0) 
     { 
      return 40 
     } 
     else 
     { 
      return 120 
     } 
    } 
} 

MyDatePickerを行うとreloadDataした後、私はcellForRowAtIndexPath機能がブレークポイントを経由して実行されて見ることができます。しかし、cell1のラベルはまだ "initialString"です。どのように問題を解決できますか?この方法では

MyTableViewScreen

+0

'てみましょう絵コンテ= UIStoryboard(名称: "メイン" を、バンドル:なし) let view = storyBoard.instantiateViewControllerWithIdentifier( "ViewController")as! ViewController'これはあなたにコントローラの別のインスタンスを与えます。これは同じコントローラではありません。あなたができることは、ブロックを使用してメインコントローラに変更を渡すことです –

答えて

0

@IBAction func actionPicker(sender: AnyObject) { 

    let storyBoard = UIStoryboard(name : "Main" , bundle: nil) 
    let view = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 
    view.lblString = String(outletPicker.date) 
    view.tableView.reloadData() 
    view.tableView.layoutIfNeeded() 
} 

新しいビューコントローラをインスタンス化しているが、あなたはそれを提示していない - それだけでメソッドの最後にdeallocedますよ。おそらく、元のビューコントローラへの参照をそのセルに渡すか、またはデリゲートパターンを使用してセルからビューコントローラに戻ったり、そのようなことをしたいと思うでしょう。

0

ライン

let view = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController 

はinstantiateViewcontrollerは、新しいオブジェクトをインスタンス化します。しかし、それはあなたが提示しているビューコントローラとの接続がありません。どちらも違うので、あなたはその文字列を取得していません。

これに対する解決策はほとんどありません。その値を格納するためにUserdefaultsを使用したり、ここに戻ったりする必要があります。また、tableviewコントローラクラスのメソッドをトリガするプロトコルを使用することもできます。

0

デリゲートメソッドで解決できます。日付ピッカーが含まれている2番目のtableViewCell、では 、pickDate機能とプロトコルを作成します。

import UIKit 

protocol pickDateProtocol { 
func pickDate(cell:YourTableViewCell) 
} 

class cell2: UITableViewCell { 

//outlets 
@IBOutlet var outletPicker: UIDatePicker! 
//variables 
var delegate:pickDateProtocol? 
var dateValue: String! = nil 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

@IBAction func datePickerAction(_ sender: Any) { 
    delegate?.pickDate(cell:self) 
    dateValue = String(outletPicker.date) 
} 
} 

そして、あなたのViewControllerであなたが持っているでしょう:

class ViewController: UITableViewController, pickDateProtocol { 

var lblString : String = "initialString" 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2 
} 

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 30 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    if(indexPath.section == 0) { 
     if(indexPath.row == 0) { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! cell1 
     cell.label1.text = lblString; 
     return cell 
     } 
    }else { 
     if(indexPath.row == 1) { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell2") as! cell2 
    cell.delegate = self 
      return cell 
     } 
    } 
    return UITableViewCell() 
} 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if(indexPath.section == 0) { 
     return 40 
    }else { 
     return 120 
    } 
} 
// Mark: - Pick your date 
func pickDate(cell:cell2) { 
    lblString = cell.dateValue 
    tableView.reloadData() 
} 
} 
関連する問題