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

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: ?

Related

iOS (Swift) - How to change any graphic (layout, size of UIView and etc.) in reusable UITableViewCell

The situation is pretty simple (as pretty common as I thought). I have an UITableView in my application that has lets say 3 rows (all are visible on the screen). Next I tap on one of them and this action changes something in all three cells (e.g. change scale of some UIView that is in this every cell) and everything is ok.
This is ok until I have only visible cells on the screen. I know that cellForRowAtIndexPath works only for visible cells. So how can we change such kind of properties (not data properties) for all the cells?
cellForRowAtIndexPath is where you prepare the cell for display. You should be doing the changes in here.
How are you modifying the view scale inside the cells when the user taps on a cell?
You should be triggering a reloadData() on the tableview which will reload the tableview and trigger the cellForRowAtIndexPath calls. You can also reload specific indexes inside the tableview if needed.
Inside your cellForRowAtIndexPath you should use some logic or state variable to control how you configure your cell. Dont dump all the code inside that function, pass arguments to your cell in a function e.g configureCell(subviewScale: CGFloat) and let the cell configure itself.
Dont forget to reset the scale of the subview in prepareForReuse() on your custom cell so that when the cell is about to be dequeued and reused, it gets its properties reset.

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/

Unable to resize an UITableViewCell containing an UIView

I have a custom table view cell. I have a UIView in the tableview cell that is shown only when the table is expanded. I toggle the height for table view cell each time on tap to show the UIView. I also need to detect clicks on some of the components of UIView.
->tablecell1
-->UIView1 height h1
->tablecell2
-->UIView2 height h2
The cell height of the cell should vary according to the size of UIView. Currently I am calling
tableView:heightForRowAtIndexPath: for varying the height on cell click. However this doesn't work if the UIViews are of variable heights and the bigger view gets clipped.
Is there a better way of solving it?
Ok so real quick tableView:heightForRowAtIndexPath: shall not be called by your controller.
The biggest problem I always run into when dynamically sizing cells is that that heightForRowAtIndexPath is called very early on in the table layout process, so essentially the height of each cell must be know prior to asking the tableview to layout. Anyway I am going to assume that you are using a custom table view cell and have placed a UIView inside there... If you haven't, do.
First: Throw a class function into the CustomTableViewCell called heightNeededForTableViewCellWithView:(UIView *)view and determine the height you would want, handle the condition where view is nil and what the default size shall be.
Second: Call this class function when tableView:heightForRowAtIndexPath: is called and since you own the datasource in your controller you can dynamically return the heightNeededForTableViewCellWithView:viewAtRow based off the view you would want to show for that row!
Third: When a user taps a cell, remember which index they tapped and call [tableView reloadData] which will call tableView:heightForRowAtIndexPath:

UISegmentedControl Selected value is missing in UITableView during scrolling

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.

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.

Resources