Reload TableView after clicking on a custom cell's UITextField - swift

I have a tableView with custom cells. Each custom cell has a textfield. When a user clicks on a textfield, I want certain new cells to appear on the screen.
This is what I'm trying:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
/*
...code to determine which new cells should be added in from my data structure
*/
self.tableView.reloadData()
return true
}
However, every time reloadData is called, I think the delegate calls textFieldShouldBeginEditing again, so I get stuck in an infinite loop and the method never returns.
Any ideas on how I can avoid this problem?

I think what you are looking for is the insertRowsAtIndexPaths call instead of reloading the whole table.
func insertRowsAtIndexPaths(_ indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation)
another stack overflow post you should look at
here's the link to the Apple docs
keep in mind you when doing this you need to keep your datasource up to date as well. update the datasource before calling insertRowsAtIndexPath

Related

How can I replace `UITableViewCell` with a `UIScrollView` or `UITableView` after the main `UITableView` is already loaded?

So I am messing around with how this feels in my app. I have a UITableView and I have the swiping/panning in place, where I kind of flick the cell away and once the cell is away I have it call a method in the same class where I have my UITableView as a property. Once that cell is swiped away and that method gets called, I want to animate the size of the now empty cell to be about 3 times the size of the cell and have a UIScrollView or UITableView be added. It will be a place to store all comments about that cell.
Basically, I would just like some ideas on how I can replace a swiped cell with a UIScrollView or UITableView and animate it to three times the size of any cell.
In a nutshell, if the text below is tl;dr:
call reloadRowsAtIndexPaths, remember which cell you've changed, update cellForRowAtIndexPath and heightForRowAtIndexPath accordingly, and you are set to go.
Full version:
When you said swiped away, did you meant that you are reloading or deleting certain row of your UITableView?
If so, I think you might be able to do what you described by calling [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; (or whatever direction you'd like), then saving the indexPaths you've changed (in your case only one indexPath), and react appropriately in the cellForRowAtIndexPath dataSource method - changing the normal cell for another one, custom-made, with UITableView or UIScrollView inside, and also reacting in the heightForRowAtIndexPath delegate method, to change the height.
The animation itself will be handled by tableView, so that's an ease for you.
Also, if you will use this method to change the contents of the cell for UITableView, don't forget to assign delegate and dataSource of that cell's new tableView, since that causes lot of troubles when forgot.

How can I stop reloadData on my UITableView from clearing the delete confirmation button?

So I have the following:
A UITableView with deletable cells (the kind where you swipe the cell then click the delete button)
The UITableView's properties (eg. a UILabel) are automatically updated (with a reloadData call on the UITableView) every second by a NSTimer (this is required)
The problem is that the reloadData call clears the delete button after swiping (within a second of course). How can I keep this from happening?
Thank you!
u can keep a boolean flag variable that becomes true if the delete button is visible and false if not and in the place where u call reloadData u can check the flag value and function accordingly
When the user swipes a UITableViewCell, tableView's isEditing property will be set to YES. You should check this property before you reload your table.
All I need to do was to set the UILabels text property from the NSTimers invocation method call and it doesn't even require a reloadData call to update the UILabel...

looping through uitableview sections to change section header frame

I have a grouped UITableView with many sections, however, I want to iterate through these sections after i tap a UIButton, so viewForHeaderInSection is useless in this case, I want to iterate through sections to change their headers frames, Im struggling to do it but to no avail until now.
I'd be grateful to anyone who helps me with this.
viewForHeaderInSection gets called every time you call ReloadData on a table view.
So you could have a Boolean that gets set to yes every time your button is pressed, and also call reloadData in the button's action. In viewForHeaderInSection, check the value of the bool, if it's true, set the size to what you want when the button is pressed, if false set it to the normal size.
I hope this helps.
After you header view's datasource iterated and changed in whichever you wish way, call
[mytable reloadData]
And your header views are redrawn. Simple and clearly you have not tried that before asking, next time do it.

iphone sdk stay the selection colour of cell in UItableview?

How to get the selection cell still in the selected state in uitableview. Actually I am pushing a view from a tableview when came back then the selection cell is deseleted. I want to be show the previous selection cell when came back also how to do that.
In didselectrowatindexpath use the method selectRowAtIndexPath:animated:scrollPosition: and the row stays selected.
when you are going and coming back from anotherview make sure that save the selectedCell and then in viewwillappear method reloaddata.In cellforindexpath write the code of selection style uitableviewcellselectionstyleblue.
first, you can store the selected indexPath in user default in the didSelectRowAtIndexPath method.(maybe you can store a indexPath that is bigger than your table view's row count to indicate initialization stage, which means that no row has ever been selected)
then, you can load the stored indexPath in the viewDidLoad method using
tableView selectRowAtIndexPath:<#(NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>
meanwhile, you can do some check (like mentioned above or something else) to check if there's nothing selected yet.(saving a bool in user defaults works, too)

AQGridView cell need to be tapped twice to trigger didSelectItem

I'm using AQGridView for displaying a TableView in grid format.
I've a problem that, randomly, the GridCell needs to be tapped twice to trigger the
didSelectItem
method.
On the first tap the cell highlights itself, but no didSelectItem method is called.
On the second tap the method is called.
Any ideas?
The Problem occurs probably only, when you reselect the previous selected GridCell.
So you have to add in your viewDidAppear the following Statement:
[self.gridView deselectItemAtIndex:self.gridView.indexOfSelectedItem animated:YES];
You can also just implement gridView:willSelectItemAtIndex instead, and return NSNotFound so nothing will be selected.