2017-01-25 3 views
-1

テーブルビューの一部ではないボタンをクリックして、テーブルビュー内のすべてのセルのラベルテキストを変更したいとします。私はインターネット上でドキュメントを検索しようとしましたが、これまでのところ解決策が見つかりませんでした。ボタンクリックでswiftを使用して、プログラムでテーブルビュー内の行のすべての行を選択しますか?

+0

のようにしてみてください私達にあなたのコードを表示し、また、簡単にあなたの問題を説明してください。 –

答えて

1

ボタンアクションでは、データソースを変更して、tableView.reloadData()を呼び出して、更新されたデータソースを使用してテーブルをリロードします。

0

あなたは3つの項目持っ例えばテーブルビューに含まれるすべての項目のデータソースを作成し、修正した後、それだけですべてのデータをリロード...

:「あなたが次に

let item1 = "A" 
let item2 = "B" 
let item3 = "C" 

let tableViewItems = [item1, item2, item3] 

func tableView(cellForRowAtIndexPath..., indexPath ...) { 
    // bind your custom datasource to tableview 
    let cell = ... 
    let item = tableViewItems[indexPath.row] 
    cell.title = item 
} 

をメインにあなたが持っている

func didPressModifyAllButton() { 
    for item in tableViewItems { 
     // here you can modify your items 
     item = "Modified ..." 
    } 

    tableView.reloadData() 
} 
+0

私はこれを使用しています。 !としてtotalRows = markAttendanceTable.cellForRow(at:NSIndexPath(行:2、セクション:0))のtotalRows = markAttendanceTable.numberOfRows(inSection:0)を0にします。 MarkAttendanceTableViewCell cel.checkBox.isChecked = true –

+0

問題は、セルを再利用しているため、例外が発生しています –

0

はオプション1:あなたのボタンアクションを得ましselectedIndexPathの配列を削除するボタンをクリックすると、すべてのIndexpathを配列に追加できます。

オプション2: テーブルビューでの選択を維持するために維持できるブール変数があります。

0

//こんにちは、この

func buttonClik(sender: UIButton) { 
    sender.selected = !sender.selected 
     tblView.reloadData() 
} 

set text of label based on button selection as follows 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if btn.selected { 
     //give text of label on selection state of button 
    } else { 
     //give text of label on normal state of button 
    } 
} 
+0

私はこれを使用しています。 0行目のtotalRows = markAttendanceTable.numberOfRows(inSection:0)

+0

問題は、セルを再利用しているため、例外が発生しています –

+0

indexpathの行のセルにそのセルを取得できます。ボタンの選択に基づいてラベルのテキストを変更できます –

1

VAR isSelected = falseを

@IBAction func yourButtonAction(sender:UIButton) 
{ 
    isSelected = True 
    tableview.reloadData() 
        
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
     let cell = tableView.dequeueReusableCellWithIdentifier(“YourCellIdentifier”, forIndexPath: indexPath) as! YourCell 

    if isSelected 
    { 
      // Set your text here 
    } 
    else 
    { 
     // Set default value 
    } 
} 


if you want to change text to previous state when user tap again on button then use. 

@IBAction func yourButtonAction(sender:UIButton) 
{ 
    sender.selected = !sender.selected 
    isSelected = sender.selected 
    tableview.reloadData() 
} 
関連する問題