Changing Segments Alters Cell Height - How do I revert back? - objective-c

I have a UISegmentedControl with 2 segments. I'm loading JSON data into a table view and each segment has different data. I first load the view and the default first segment shows up - data is there and cell height is perfect. When I tap on the second segment, all is well, the data loads properly and the cell height now matches this set of data. However, when I go back to the first segment the cell height is from the second, which is not what I want. Ideally the cells will resize for the first segment data like it originally did.
When I return to the first segment, how can I re-adjust the cell height so the data displays properly again?

You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method. and return the hight you want there.

Related

Wrong UITableViewCell height for some cells at first loading

I am using UITableViewAutomaticDimension to calculate height for my table view cell. It works fine for me. But for some cells, the height returned is 44 rather than dynamically calculating height. But on scrolling up an looking back to the same cell, the height is recalculated perfectly. So I guess I do not have any faulty constraints because, after I scroll and correct all cell heights manually, no constraint breaking warning are shown afterwards.
Edit
The below given is the screenshot of image(got messed up when cropped, but can see the issue from the profile image.). The first cell height is calculated correctly. But the rest are faulty.
I have added the following and everything worked fine.
(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
In my case it was something wrong with layoutMarginsGuide top and bottom anchor. I use it for cell subviews and set preservesSuperviewLayoutMargins = true for its subviews. This caused the mistake when system tried use systemLayoutSizeFitting to calculate cells height first time.

Design tableViewCell( overlaps table view cell)

How to design this table view cell..
Cell overlaps the another cell.
Please help me
Set the y offset of the pictureImageview to be negative i.e -50.0f or so, the image will appear as starting from the cell above.
I just tried AppleDelegate's solution but didn't work, because the top of every image is cut, maybe because it is beyond the cells bounds, even with clear color for background.
A different approach would be to believe that it's just a visual effect, and cells are not overlapping. This is possible by giving a different height for every cell with next code :
- (CGFloat)tableView : (UITableView *) tableView
heightForRowAtIndexPath : (NSIndexPath *) indexPath {
// RETURN A DIFFERENT HEIGHT FOR EVERY CELL DEPENDING ON INDEXPATH,
// INCREASING IT GRADUALLY.
}
In the next image, the black lines look like cells' top border, but maybe that's the effect, and the real limit is the red line :
Thinking this way, only the first cell requires a special treatment for the green titles.
Try setting constraints in storyboard and then keep the UIImageView's size/frame of image same when the image is set to UIImageView. Good Luck!

How to detect how many cells are visible in a UITableView

My table view covers part of the view. If I have 20 objects, I am displaying all of them in a tableview. But I want to know that how many cell are loaded that are visible to the user.
(i.e. first 5 cells data is visible for me: when I scroll down, the remaining cells will load. Here I want to know without scrolling how many cell are loaded.)
Is this possible?
You can use [tableView visibleCells] count], which returns numbers of the table cells that are visible, if I understand correctly what you want.
The below code will give you the array of indexpath of cells currently visible
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
after getting the index path you can easily access the cell at a particular indexpath
Its usually : (tableView's height / cell row height ) + 1
Can you take the size of the visible part of the table, and then the size of one cell, and divide them to know how many of them fit in the screen?

How to increase the row height in NSTableView based on the text Content?

How to increase the row height in a NSTableView based on the text Content.Row height should be increased when there is more text and the row should shrink when the text is deleted.
I implemented the textDidChange notification in my subclass of NSTableView,but I did not find any method which will increase the row height. setRowHeight of NSTableView will increase the height of every row in the tableview. I would like to a variable row height.Can I have any sample code or any pointers to accomplish this.
I found the following link
http://cocoadev.com/forums/discussion/1851/calculating-row-heights
But I am not able to find any example on this!. Any example on this would be a great help for me!
Tried to implement the following deleagate
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
by calculating the string length,Now I am able to increase the row height ,but the NSTextFieldCell height is same. How can I increase the height of the textfield when the row height changes.
corbin dunn gave a solution:
fix your autoresizing/AutoLayout coinstraints so the text view sticks to its container,
calculate the row height -tableView:heightOfRow: with the help of a custom NSTableCellView
object.
You can increase height of tableviewrow in the method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {} . Call [tableView reloadData] in textDidFinishEditing to reload the table .

Show the complete sections of rows in my table view after scrolling is done

i have table view(it has sections). at a time it will show only 4 images/rows(the height of table View is set according to that). when the scrolling is done, it will display 5th row half section,6th row full, 7th row full ,8th row full , 9th row half section as usual. however i don't want to show the half section of any rows. i need to show the complete sections of rows in my table view(it may be Rows 5,6,7,8 or Rows 6,7,8,9) after my scroll function is done. is any way to do it?any property for tableView to do it?
I think you should combine next methods:
scrollToRowAtIndexPath:atScrollPosition:animated:
Scrolls the receiver until a row identified by index path is at a particular location on the screen.
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
scrollViewDidEndDragging:willDecelerate:
Tells the delegate when dragging ended in the scroll view.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
scrollViewDidEndDecelerating:
Tells the delegate that the scroll view has ended decelerating the scrolling movement.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView