UITableView scroll both vertically and horizontally - iphone

I have too many columns in my table and too much data too. For that reason I need to scroll my UITableView both vertically and horizontally. Is there a direct way to do so or I need to go through sources like Easy tables?

You can use an UIScrollView and then inside add the UITableView. This UIScrollView will have the same size that your UITableView have now, but the UIScrollView contentSize property will have the same height but it would have a greater width.

I have a similar situation: a lot of columns and rows, the tableView needs to be horizontally scrollable. My solution is like that:
predefine a column header with width same as the desired tableView, say 537
top view (320 x 455)
+ scroll view (320x455), content size = (537x455)
+ view as a container (537 x 455)
+ column header (0, 0, 537, 44)
+ tableView (0, 44, 537, 455-44)
Create a function to compute these numbers and reattach the frames to each object
after tableview reload, call this function

Here is a trick to do both vertical and horizontal scrolling with a UITableView.
Playing with width and contentsize to make it work:
https://stackoverflow.com/a/41230797/860488

Related

Random table view cell height is not working perfectly

I am designing a chat app in which I am using the UITableViewAutomaticDimension property of table view when I am setting the label lines to 6 then all cells height is according to the text which label contains but when I increase the number of lines for label then some cells have extra height. Even when scrolling it then cell height again changed.You can check the image so that you can understand it easily thanks.
You need to give label constraints from top and bottom, rather than center vertically.
Give vertical content hugging priority

Aligning rows in UITableView

This may sound a newbie question, however I'm new to iOS dev.
I've got a UITableView on my iPad app. TableView has obly three rows, is there a way to tell UITableView to view rows vertically centered, i.e. to not from the top to down.
Figure out the sum of the heights of all 3 rows, call it MyTotalHeight.
float MyTotalHeight = heightOfRow0 + heightOfRow1 + heightOfRow2;
Set your
tableView.frame = CGRectMake(start_X, start_Y, tableWidth, MyTotalHeight);
If you want the contents of each row/cell to be centered vertically within the cell, this will depend greatly on what is in the cell. You will need to calculate the height of the content and then center that content vertically within the cell by adjusting it's frame.
You may want to try the UiTableView.sectionHeaderHeight property. Play with the number until the cells are centered vertically. If your using a plain table view, I don't know how well this will work for you.
--John

scaling UIScrollView contentSize to fit UIView with variable-height UITableView inside

I have a uiview with a uiscrollview as a subview in a nib.
Then, I load another uiview with a uitableview as a subview of this other uiview (among other elements inside this view) as a subview of the uiscrollview.
That is, UISCrollView -> UIView -> UITableView (and some other views inside the UIView).
And I need to do the following:
Once I know the final height of the tableview (depending on the number of cells and height of them) I need to resize the uiview containing it and, in turn, resize the contentSize property of the containing scrollview.
But I don't know at which point the final size of the tableview is known (because it can dynamically change size depending on the amount of text its cells will hold) and neither do I know by how much will the tableview exceed the uiview (which by default is 320 x 460).
I've tried setting the view containing the tableview to a height of 900 on viewdidload and setting it to sizeToFit, hoping it would shrink as tablecells are added to the tableview (assuming as cells are added the tableview's frame would scale appropriately). But this doesn't seem to work.
Any thoughts?
Thanks!
I just ran into a similar situation (UIScrollView -> UITableView) and was banging my head against it for hours. The short answer is this: the correct sizes to use as the basis of sizing things out were not available until my ViewController (governing the UIScrollView) was sent viewWillAppear:. Before then, I found that the values were set, but often wrong. (E.g., my first attempt at doing this was in viewDidLoad:, and the sizing values were changed there, but they were wrong!)
So my solution was thus:
In viewDidLoad: cause your enclosed UITableView to reloadData once you have your data in place.
in viewWillAppear: do the following (presuming tableView is the enclosed UITableView and scrollView is the enclosing UIScrollView):
tableView.frame = (CGRect){
tableView.frame.origin,
tableView.contentSize
};
// Finally, set content size based on the last element in the scrollView
// in my case, the last element IS the tableView, yours might be different
scrollView.contentSize =
CGSizeMake(scrollView.width
- scrollView.contentInset.left
- self.scrollView.contentInset.right,
tableView.top
+ tableView.height);
N.B., I was getting some very weird numbers when I checked them before viewWillAppear:; e.g., the tableView.contentSize seemed to be the product of default rowHeight * number of rows, ignoring my custom row heights and excluding any separators! Very odd.
This is just a guess, but since UITableView is a subclass of UIScrollView, can you use the contentSize property to see its overall size after a call to reloadData? You could use the size of the tableview to resize the UIView and the contentSize of the scrollView.

Height of UIPicker

I've been trying to change the height of a pickerView. I would like it to show a single row instead of the default five rows.
help me plz
Better u can use PickerView Frame like this,
Pickerview.frame = CGRectMake(30,100,170,200);
You can't. UIPickerView can show either 5 rows or 3 rows, depending on the height of the frame you pass to the -setFrame: method. When I need a short UIPickerView, I'll pass a height of 100. The code in UIKit will adjust that height to be the closest allowed value.
I'll also note that this is for a very good reason. A one row pickerView, as has been noted elsewhere, would be fiendishly difficult to scroll. Also, the number of visible rows must be odd so that the currently selected row will be centered vertically. This means 3 or 5 rows, because 7 would make the thing way too tall to be practical on an iPhone.
As an aside, this also works with UIDatePicker for the same reasons. The exception is when the date picker is in the UIDatePickerModeCountDownTimer, which does not support the 3-row variant.
If you absolutely MUST have a one row picker view:
Create a UIView sized 170 wide by 55 tall.
Set the view properties clip subviews = YES
Add a UIPickerView as a subview of the UIView. Set its origin to 0, -80.5.
Then add a UIImageView sized 170, 55 as a subview to the UIView. Set its image to the one shown below.
(You can modify the image and make it stretchable to resize it horizontally)
Final Result:
did you try to implement numberOfRowsInComponent: ? If this doesn't change the size, did you try to set the picker's frame ?
ps: I agree with #Ritheesh#BoltClock, one row isn't a good idea

How do I decrease cell size to 280*35?

I need to decrease the table view cell of navigation controller to 280*35. I have tried to with cgrect, but it's not working...
How can I set it?
The width of a UITableViewCell is always determined by the width of the UITableView that contains it.
You can change the height of your Cell by using UITableViews delegate message tableView:heightForRowAtIndexPath:
You can set row height programmatically, as tonclon pointed out, or, if all rows need to get the same height, set it in Interface Builder as a propery of the UITableView, in de Inspector window under the size tab. It says "height" right at the top, that's the row height.
This is easier and (presumably) faster.