Issue with Uitableview: Action Only Works Every Second Time

Issue with UITableView: Action only works every second time

Change didDeselectRowAt to didSelectRowAt

UITableView reloadRows only works every other time

Thank you for the suggestion Paulw11, with debugging and persistence I was able to resolve and correct this.

Problem Source

  • Custom cells, initialized on viewDidLoad() in vc

Problem Solution

  • Have tableView(cellForRowAtIndexPath) return the cell directly, not by dequeueReusableCell()

I am not sure entirely why this corrected to problem but it does somewhat make sense, dequeueReusableCell() returns a new cell when the 'identifier' is not found!

UITableView dynamic cell heights only correct after some scrolling

I don't know this is clearly documented or not, but adding [cell layoutIfNeeded] before returning cell solves your problem.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
NSUInteger n1 = firstLabelWordCount[indexPath.row];
NSUInteger n2 = secondLabelWordCount[indexPath.row];
[cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];

[cell layoutIfNeeded]; // <- added

return cell;
}

didDeselectRowAt gets called after two taps

It seems you are using didDeselectRowAt. This means that this code is called not after the tableCell is selected on the first tap, but when it is deselected on the second tap. Therefore, your code should not be this:

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// delegate?.onSelect(users![indexPath.row])
print("selected %d", indexPath.row)
self.performSegue(withIdentifier: "segueToUserForm", sender: users![indexPath.row])
}

But this:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// delegate?.onSelect(users![indexPath.row])
print("selected %d", indexPath.row)
self.performSegue(withIdentifier: "segueToUserForm", sender: users![indexPath.row])
}

Of course, didDeselectRowAt is useful in specific situations, however, it seems that it is not useful in your case. In short, replace didDeselctRowAt to didSelectRowAt.

Why handle clicking on TableView Swift IOS worth cell rows are the previous rows clicked?

When you click on a row, that row is selected — and the previous selected row is deselected. Well, you have implemented didDeselect, so the previous selected row is displayed. Instead, implement didSelect.

Swipe to delete from TableView not working

The UITableViewDelegate implements the method

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath)

which can be used to configure a swipe action - a delete in your case. Here is a example I use in one of my projects:

extension ExampleViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive,
title: "Delete") { [weak self] _, _, complete in
tableView.deleteRows(at: [indexPath], with: .automatic)
complete(true)
}
deleteAction.backgroundColor = .red

return UISwipeActionsConfiguration(actions: [deleteAction])
}

// instead of a gesture recognizer you can use this delegate method
// it will be called whenever you tap on a cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// whatever you gesture recognizer did
}
}

Since your GestureRecognizer on the entire view gives you trouble, I'll recommend using a third party implementation or apples public API to handle dismissing your viewController instead of implementing it yourself. In that case you can use the UIModalPresentationStyle of your viewController.

func startExampleViewController() {
exampleViewController.modalPresentationStyle = .formSheet
previousViewController.present(exampleViewController, animated: true)
}


Related Topics



Leave a reply



Submit