2016-12-27 4 views
0

私はを表示するViewControllerUITableViewを持っており、次のコードに基づいてシミュレータで5回実行しています。ボタンが押されたことに基づいて、UITableViewでラベルのテキストを変更する方法は?

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 

    @IBOutlet weak var myTableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int 
    { 
     return 5 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OutsideCell 
      myCell.titleLabel.text = "Default" 
    return myCell 
    } 

    @IBAction func firstButtonPressed(_ sender: Any){ 


    } 

    @IBAction func secondButtonPressed(_ sender: Any) { 

    } 

} 

また、私はFirst Button場合firstButtonPressedUITableViewSecond ButtonsecondButtonPressed代わりにDefaultとしてテキストを表示したいとViewController の下部にある2つのボタンを有します。これはどのように可能ですか?

答えて

2

ボタンをタップしたときにテーブルビューの各セルをループすることができます。

for cell in myTableView.visibleCells() { 
    cell.titleLabel?.text = "First Button" 
} 
+0

は、それが動作しますが、それを感謝、それが役に立てば幸いあり、とにかく私の代わりにデフォルトの初めてのときのビュー負荷を表示する最初のボタンを作ることができますか? – leaner122

+0

cellForRow関数では、Defaultの代わりに別のテキスト値を使用してください。 – TheValyreanGroup

0

あなたはeventButtonPressedとして一般的な方法を使用して、ストーリーボードを通じて押されたボタンを決定するために、タグの属性を使用する必要があります。

@IBAction func eventButtonPressed(_ sender: UIButton) { 
    if (sender.tag == 0) { 
     //First Button 
    } else if (sender.tag == 1) { 
     //Second Button 
    } 
} 

多くの場合、スイッチオペランドを使用できます。

switch sender.tag { 
    case 0: 
     print("First button pressed") 
    case 1: 
     print("Second button pressed") 
    default: 
    print("Default button pressed") 
} 

関連する問題