iOS : UITableView - Adding columns to your table - iphone

I'm looking to change the number of columns my tableview has without subclassing it. Any suggestions?

I think you can find your happiness here: http://usxue.is-programmer.com/posts/14176.html
With that you can create a Grid and have multiples columns

If you don't need to select individual columns the you could just subclass UITableViewCell to make it LOOK like it has more than one column.

Doing this without subclassing anything would need to be done as follows:
1) Place a UIScrollView in the view.
2) Put n amount of UITableViews embedded in the UIScrollView where n is the amount of columns desired.
3) Turn scrolling off in all of the UITableViews.
4) Make all of the UITableView frames equal to that UITableView's contentSize.
5) Set the UIScrollView's content size equal to the largest UITableView's frame.
I have not tested this because I would definitely recommend subclassing either UITableView or UITableViewCell, but this would be one way of doing it.

Related

TableView height in Swift

My tableview add blank space to the end of the cells.
I don't know why, how do I fix this problem? (For ex I have 50 cells, two of them you can see here because this is last two cells)
Hard to know without seeing your code, but because your cells have varied heights, you should probably start by implementing tableView:heightForRowAtIndexPath:
If you end up wanting more manual control over the table's height, take a look at its contentSize property.

How many UILabels can be added to a View

I want to add about 300 grids to a UIView,It is OK if I use 300 UILabels?
Or,besides UILabel,any better way to deal with it?
Grid means what you can make a UITableView with 300 rows and you can make any number of labels in your UIView
Perhaps start here. Other than that, I don't see a problem with UILabels, with that count, it might get slower though. Imagine how many objects you will be rendering...
If you create a UITableView with reusable cells, only the visible cells will be populated at any one time, so only those UILabels (assuming one or more per cell) will actually exist at any one time. Not a problem.

how to subclass UITableViewCell if the content/height of each cell varies

I have a UITableView. The height and content of each cell of the table view varies, depending on the data feeding to the cell. To give an example - some might have pictures, some might only have text; some might have 1 picture, some might have more than 1 picture. So, I am thinking about subclassing the UITableViewCell so I can use the subclass in other table views that I might have.
Is there some sample code that I can follow to achieve that goal - subclassing UITableViewCell, varied content, varied height?
This has been asked many times.
Also, you don't need to subclass to get variable row height, nor to put custom controls into the cell, but for the latter case subclassing may be convenient.

iPhone Dev - How to make a UITableView the exact height to fit all of the cells in it

I have the following:
I would like to make it so that the tableview stops right at the "So" cell, instead of having all the blank cells under it (the big plan is to have a bunch of multiple choice questions like the one in that pic, all on top of one another on one scrollview). I'm guessing I need to set the tableView's frame, but I was wondering if there was an easy way to calculate at runtime the exact height of the portion of the tableview where the cells are implemented (the ones with text in the pic). It's tricky because I made it so that each cell's height can change to accommodate the amount of text in it. Anyone have any advice?
There are two appproaches you can take.
Best and easiest.
1. Add a footer view to the table so it will not draw the rows after the last row. a blank footer view will do.
//Add empty view to hide trailing row seperators
UIView *emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
emptyView.backgroundColor = [UIColor whiteColor];
[tableView setTableFooterView:emptyView];
[emptyView release];
2.Check how many rows you have in the tableview and calculate the tableheight and change the frame of table view.
There isn't a tableView:sizeOfAllCells: function if that is what you are looking for. From your question it sounds like you to adjust the size of your table so you can put other content on bottom of it.
You are correct is saying that you want to alter your UITableView's frame property to match the height.
[table setFrame:CGRectMake(0,0,table.frame.size.width,heightOfCells)];
There are a few things you could do though. If you had an array of all the cells, you could loop through it real quick and add the height of each of them. You could (I don't particularly recommend it though) loop though the number of sections and rows you have, using tableView:cellForRowAtIndex: instead of having an array of cells, and get the heights that way. If each cell is the same height (sounds like its possible it wont be) you could just do some simple math to figure out the height. Lastly, you could keep a dictionary or array of heights and update it in the tableView:cellForRowAtIndex: so if the content changes, it updates the size, but then you would have to call a reloadData. Those are just a few ideas of ways to solve this particular problem.
Here's a hack (so wait to see if a better solution comes along):
Use numberOfSections to get the number of sections (if you later change to grouped style), and then add up the heights of rectForSection: for each of the sections (in plain style, just take rectForSection:0). Maybe add a bit to this for the separators.

UITableView Did Load (Done drawing the cells)

Question
How can you detect when the Table View is done drawing the cells?
Issue
I got two labels within the contentView of an UITableViewCell. The size of these labels are dynamic. I was able to do so by subclassing UITableViewCell, in the drawRect method I adjust the frames of the two labels depending on their content. Now I want to align all the second labels.
My Thoughts in Steps
Determine the content in the table view and let it load automatically.
Run through the table view cells and determine the x position of the second label within the UITableViewCell that is the furtherest away.
Store this x position and when any cell is drawn use this x position to place the second label.
The problem is that if I use the following code:
for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
UITableViewCustomCell *cell = (UITableViewCustomCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
NSLog ([cell.labelTwo description]);
}
The second label has not yet been drawn, meaning I can't determine the size of the frame and thus can not find the proper x position to align all second labels.
I have tried subclassing the UITableViewController and looking at events such as viewDidLoad and viewDidAppear unfortunatly also in these events the cells aren't drawn yet.
What I Want ...
What I want is for the table view to draw the cells at least once so I can determine the sizes of the labels within the table view cell. I thought to accomplish this by looping through all the cells with cellForRow, but although it successfully returns the cell the content is not drawn yet meaning the frame remains with a width of zero.
Does anyone have a solution?
Thanks in advance,
Mark
Try calling sizeWithFont: on the contents of these labels to get the max width before you draw anything. You should be able to use it later in your cellForRowAtIndexPath: to adjust the width as you need.
I would recommend you reconsider using UITableViewCellStyleValue2 cells instead and attempt to configure the textLabel and detailTextLabel. I had a similar situation and this is how I did it.
First off, you really ought to just pick an explicit, fixed position at which the first label ends and the second one begins, based on what you know about the minimum and maximum lengths of the text that will be put in those labels. That would eliminate this problem entirely.
But if you want a solution: use the sizeWithFont: method or one of its cousins (see the Xcode docs). Specifically, loop through the values that will go in the first labels, apply sizeWithFont to each, and keep track of the largest width you see. (I'm assuming you have access to the values before they go in the cells; since they're dynamic, they must be passing through the table view controller, no?)
Now you have the value you seek, without having to perform the extremely wasteful operation of creating a bunch of cell objects and never using them for their intended purpose.
I think what you need to do is to add a viewController to the have the UITableViewController control the UITableViewCell itself so that you can capture the events of the labels loading. The viewController will have references to both labels so it can adjust them accordingly in response to -viewDidAppear.
I've never done this but a UITableViewCell is a view like any other so you should be able to set up a controller for it. You might need to manually activate the controller since you have no navigation controller to do it for you in this context.