Back to Table View from Detail Page Without Reload, Swift

Back to table view from detail page without table reload

You can maintain a simple flag base mechanism to accomplish this. Declare a flag within your ViewController as follows

class YourViewController: UIViewController {

// MARK: Internal Variable
private var shouldReloadDataOnViewWillAppear: Bool = true

override viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if shouldReloadDataOnViewWillAppear {
// your code for reloading data
}

shouldReloadDataOnViewWillAppear = true
}

Now you can set shouldReloadDataOnViewWillAppear to false inside prepareForSegue or didSelectRowAtIndexPath which will restrict the data loading next time Your view controller re-appear on main window.

How do I refresh a table view after returning from a detail view?

Use [tableView reloadData] - that will make the tableview run though the rows and rebuild itself.

How to update tableview cell without reload data ?

Try using tableView.cellForRow to get the cell and configure it directly yourself only with the modified data:

// this should give you currently visible cell for indexPath
if let ratableCell = tableView.cellForRow(at: indexPath) as? RatableCell {
// instead of telling tableView to reload this cell, just configure here
// the changed data, e.g.:
ratableCell.configureRate(10)
}

Swift reloading cell without reloading whole tableView

You are doing the same in both code blocks. And in the worst case, you are asking your tableView two times for a cell - just because the class didn't match (but your needs are the same for both).

Interfaces (protocols in ObjC) let you define method signatures (and properties in Swift?). And that's what I'd do in ObjC: Define a protocol (interface in other languages) and simply require any cell for this tableView to implement it.

This attempt also eases further addition of other cells, which provide almost the same features.

UITableView Refresh without scrolling

I am showing if only one row is being added. You can extend it to multiple rows.

    // dataArray is your data Source object
[dataArray insertObject:name atIndex:0];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView reloadData];
[self.tableView setContentOffset:contentOffset];

But for this to work you need to have defined - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath the method. Or else, you can directly give your tableview row height if it is constant.

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