Reload Tableview After Clicking The Button Present Inside One of The Tableview Cell in Swift

Reload tableview after clicking the button present inside one of the tableview cell in swift

Very simple to solve it,

every time you click a button, you record the index in the controller, then reload the tableView.

in the cell configuration, if the cell's indexPath matches the record index, its sub table show ,the rest's sub table hide.

If you hit the button two times, the record index in the controller is nil, then reload tableView, and all cell's sub table hides.


I name the pattern, mark & config


Your code is of chaos

from your code , I think LcodeTableViewCell is an object , not a class.

@IBAction func dropDown2ButtonAction(_ sender: UIButton){

if buttonTag == getIndexPath()!.row{

buttonTag == getIndexPath()!.row, will be always true.

// BAD CODE ...

Reload UITableView when button clicked in custom UITableViewCell

Use a delegate for this.

Set the delegate for the custom cell in tableView(_:cellForRowAt:), then call tableViewDB.reloadData() inside the delegate's function.

TableVC

class TableVC: BaseController, DBDelegate, PLDelegate, DailySpeakingLessonDelegate {
@IBOutlet weak var tableViewDB: UITableView!

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dailySpeakingLesson = tableView.dequeueReusableCell(withIdentifier: "cellId") as! DailySpeakingLesson
dailySpeakingLesson.delegate = self

return dailySpeakingLesson
}

func dailySpeakingLessonButtonPressed() {
tableViewDB.reloadData()
}
}

DailySpeakingLesson

class DailySpeakingLesson: UITableViewCell {
weak var delegate: DailySpeakingLessonDelegate?

@IBAction func buttonPressed() {
delegate?.dailySpeakingLessonButtonPressed()
}
}

Delegate

protocol DailySpeakingLessonDelegate: class {
func dailySpeakingLessonButtonPressed()
}

How to reload tableview after submission of a page of type present modally in swift

You can create a protocol and an object of the protocol in your presented controller as follows

 protocol DismissView{
func updateTblData()
}

var delegate: DismissView?

When you dismiss the view call the method of the delegate as follows:

if let delegate = delegate {
delegate.updateTblData()
}

Now in your controller from where you presented the screen, please conform to the protocol and provide definition of the method.

Also when presenting the controller please assign the delegate to self as below:

secondController.delegate = self

The screen from which you present could look something like below:

class FirstViewController: UIViewController , DismissView {
@IBOutlet weak var tbl: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

}

@IBAction func click(_ sender: Any) {
let controller = storyboard?.instantiateViewController(identifier: "new") as! NewViewController
controller.delegate = self
present(controller, animated: true)
}
func test() {

tbl.updateTblData()
}

}

Swift - can't reload Data from TableView after add button action

Add group item to your groups array and after that reload your tableview as shown below:-

func addNewGroup(_:UIAlertAction) -> Void {
let group = Groups(groupId: UUID(), groupTitle: groupsTextField!.text ?? "")
do {
try group?.managedObjectContext?.save()
self.groups.append(group)
groupsTableView.reloadData()
} catch {
// TODO: error handling
print("Could not save group")
}
}

Swift - Table View Cell Button to perform actions and then reload TableView

The easiest way to do this would be to create a property for a callback on the table view cell. In the cellForRowAtIndexPath(_:) method, give the cell a callback that reloads the table view. When the IBAction is fired on the cell from tapping the button, simply invoke the callback that was given.

Reloading Tableview Cell after returning from Detail View Controller - Swift

You need to send the array of preferablly classes item so the edit is reflected to original array , then after editing it inside the detailVC then back inside the PrevVC either

override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}

or use delegate/notificationCenter to reload the table , if you want to reload only the edited indexPath then create a var and hold it when didSelectRowAt is clicked and reload only that index inside the mentioned ways

var index:IndexPath? 
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
index = indexPath
// segue / push detail here
}

With

if let ind = index {
tableView.reloadRows(at: [ind], with: .none)
}


Related Topics



Leave a reply



Submit