UISegmentedControl Selected value is missing in UITableView during scrolling - ios

I have a UITableView with around 15 cells. In each cell, there is a UISegmentedControl.
When user selected a value of UISegmentedControl in the first cell, scroll down and scroll back to the first cell. The selected value is missing.
I found that it called -cellForRowAtIndexPath: frequently. While the cell dismissed from the screen, it will call -cellForRowAtIndexPath: again when the cell is back to the screen. And the selected value of the UISegmentedControl is reset.
I am currently using iOS8, and I don't have any problem with previous iOS version.
Thank you very much.
Ben

Multiple calls for cellForRowAtIndexPath is normal, the app dequeues offscreen cells. Make sure you save the state of the UISegmentedControl and update the segmented control appropriately in cellForRowAtIndexPath the next time it appears onscreen.

Related

UIPickerView in UITableViewCell showing behind next Cell

I have a tableview with a number of custom cells. The problem I am having is that the cell with the pickerview is 'bleeding' behind the neighboring cell.
Once I push another view controller and pop back, the neighboring cell is properly opaque until I manipulate the picker, at which point the issue materializes again.
The cell is, in fact, opaque. I have set it as such in both the storyboard and code. I have also tried setNeedsLayout and layoutIfNeeded in cellForRowAtIndexPath.
Set clipsToBounds property to true to each cell in the method cellForRowAtIndexPath.
You can set this property in the Storyboard if you are using prototypes cells.
You need to provide more information.
But if I understand correctly, you should hide the cell that contains the picker and only show it when the user will input.
Here is a tutorial on how to hide the cell, like the calendar iOS app does.
http://www.appcoda.com/expandable-table-view/

TextField superposed in tableview

I initially load a hidden tableView with some data and show this when I press a button, simulating a drop down menu.
In the same view, I have a UIButton which when pressed, programmatically creates another UIButton and a UITextField.
The problem is that when I load again, the tableView and all the textFields and buttons are superposed.
This is what happens:
My question is what can I do for keep the tableView in front of in the view.
When you do -addSubview:, the view is added as the top-most view.
Which is the reason why your newly added textFields are appearing above your tableView.
The quickest way to solve your issue would be to make the tableView the top-most view when you're planning to show it.
Example:
[yourTableView setHidden:NO];
[self.view bringSubviewToFront:yourTableView];
Assuming that self.view contains yourTableView
What you are experiencing is a cell reusability issue.
If you are adding controls on cells, when they get reused They will keep the textFields which gives you this strange juxtaposition result.
You have to take proper care of removing the textField which makes things more complex.
Solution is to subclass UITableViewCell so that you have prototype cell with textfield and another one without.

How do I get selectRowAtIndexPath: to work with willDisplayCell:?

First off I have a custom tableview subclass that uses a uiview subclass with drawRect: to handle the cell's content view. Now since I need a the cell's selected to be a certain color I set the cell's selectedBackgroundView in willDisplayCell:
This works great until I need to scroll to and highlight/select (but not call didSelectCell) upon showing the tableview. When I try to call selectRowAtIndexPath: the tableview scrolls to the cell but the selected color is only shown in the cell where the contentView isn't (by the disclosure arrow).
Now I believe this is because selectRowAtIndexPath: highlights the cell then scrolls to it. And willDisplayCell needs to be invoked before the cell is highlighted. However willDisplayCell is called "only when animating rows as they slide in or out" i.e. after the highlight and before the scroll.
I confirmed this by calling scrollToRowAtIndexPath: then a selectRowAtIndex:animated:scrollPosition: wrapped in a perfomSelector:afterDelay:
How do I fix this without using perfomSelector:afterDelay: ?

UITableView scroll cell by cell

I want to scroll a UITableView cell by cell (like the date picker, for example), so that the first visible row is always displayed entirely. How can I do that?
Use the scroll view delegate method scrollViewWillEndDragging:withVelocity:targetContentOffset: (your table view delegate is already the scroll view delegate) so that you can inspect the proposed targetContentOffset and modify it to ensure that the target first row is fully visible.

UISearchbar inside UITableView as the last cell hiding the keyboard

I have a UITableView in which I have several custom cells and my last cell contains a UISearchbar in it. I have wired up the appropriate delegates and referencing outlets and can get the keyboard to show up.
The problem I am facing is that the searchbar is at the very bottom of my tableview i.e. it is part of the very last cell. So even though the keyboard gets shown, the searchbar is hidden below it as I suspect it is unable to scroll to that location since its the last cell in the view.
Does anyone have any thoughts on this and how to overcome this situation.
The easiest solution is to have your view controller be a subclass of UITableViewController. This type of view controller will handle issues exactly like these.
If you cannot, you should listen to keyboard will show/hide notifications and set contentInset and scrollviewIndicatorInsets bottom of the table view to the keyboard height. Then you can scroll the specific cell into visible. When the keyboard hides, set the bottom to the previous value.
Yes....Two ways
1) either you can change the frame of whole tableView and pull the whole table up by decreasing the y position of tableView
[tableView setFrame:CGRectMake(100,100,100,100)];
or
2) You can change the contentOffset of the table to programatically scroll the tableView's last cell so that it is visible to the user.
[tableView setContentOffset:CGPointMake(0,400) animated:YES]
Hope this will help you.

Resources