I have a tableview using a custom cell per row. Per cell/row I have 3 Labels. The first label of the 3 contains a number(day). This is the same on each row. The problem is some Labels contain the same value on the next row and what I would like to achieve is that if the value on the next row is the same as the value on the current row. The Label on the next row becomes hidden.
How to do this?
There are 2 scenarios you can deal with.
The first one.
You may want that when your tableView appears it just only contains cells with different values inside the labels. In this case, like rmaddy said, you have to properly filter the tableView data source, in order to avoid repeated values, and then show the tableView.
The seconde one.
You want to show the "initial" tableView with repeated values and then update it to show only cells with different values inside the labels (maybe through an animation). In this scenario, you can filter the source data and then call [tableView reloadData] or you can detect which are the cells that have to disappear (that is retrieving their NSIndexPath) and then update tableView through
[tableView beginUpdates]
// Here use deleteRowsAtIndexPaths:withRowAnimation:
[tableView endUpdates]
Related
Can anyone help me with trying to figure out how to structure my code so I can enter in a value into one cell and have other cells calculate based on that cells value. For example, I want to have a static table view with 4 cells. The first cell being a total amount and the other 3 cells will automatically calculate when a user enters data into the 3 remaining cells. The data has to be dynamic. For example, if the total cell is equal to 12.00 and a user enters 2 into one of the three cells, the numbers 5 and 5 get placed in the other two cells automatically. I am think some kind of loop but can't figure out how to structure it.
You should use a UITableView with Prototype cells, not static, so you can reload it and have dynamic values in the text fields. If you want an example, let me know.
I would like to build a TableView where when the user clicks on a cell it expands to show more information. My question is how do I use Autolayout to arrange the multiple items in each cell.
Each cell will always be the same size, whether it is collapsed or not, so the sizing isn't dynamic.
The first problem I have is how to use Autolayout to arrange all the items in the cell. Before Xcode 7 I was successfully using Autolayout where I would pick a label-button-view to arrange, click Editor > Align > Trailing/Leading/Top Space, to.. etc. This is now greyed out and I don't know how to replace my old strategy.
Each cell has two rows of items. The first row shows all the time, the second only shows on collapse. Below is a picture of how the cell will look when it is collapsed:
The first row is a bit trickier because outlet1 and outlet 2 will have variable sizes. I would like 'label' to come right after label1, no matter how long or short that outlet happens to be. As arranged currently, there is a variable amount of space between the two.
What I'm looking to achieve in row 1 is basically exactly like Venmo:
Notice how 'paid' conforms to the size of the two names in the first row.
The second row has two buttons and an outlet which will always be the same size.
To sum up - how can I layout these elements in the UITableView for iphones4 thru 6S - and then how do I make this cell a collapsible cell? The construction of these cells seem to work as a system, not isolated from the whole - which is why this is a 2-part question.
is this what you want to achieve?
I have a tableview with two sections. I have loaded values from same array to the tableview. It is working fine, and I have customized the alpha value of the cell text in section 1, 1.0 and in section 2, 0.5. When I scroll down the table it is working fine. But when I scroll up (bottom to top), the alpha value of the section got changed to 0.5 (that is the value of section 1). Then the alpha value in both the sections are 0.5. Why it is happening so? Does anybody know the solution, please let me know.
It is probably because you are reusing the cells, but not resetting the alpha values in the cell you are reusing.
If you have a line with
dequeueReusableCellWithIdentifier
in your cellForRowAtIndexPath method, then this is retrieving a cell for reuse - if you use this reused cell then you need to make sure you reset the alpha values of the background accordingly because it could be reused for a different row to the one the cell was originally created for.
I have a set of 100 rows, pretty similar to values which can be selected in a picker. When the user scrolls the table, I want the rows to be appended like an forever-ongoing assembly-belt. So when the user scrolls down and reaches the row 100, and scrolls even further, the table view will show again row 1, and so on. Reverse direction same thing.
My thoughts:
don't display scroll indicators (they would make not much sense, probably)
what value to return in the numberOfRows delegate method? This infinity constant?
in cellForRowAtIndexPath: simply wrap the index around when it exceeds bounds?
Many apps do this. The idea is that the beginning of the table is always the same, but you keep adding to the end, so the table just keeps growing.
Let's say you first have 100 data elements. Your numberOfRows returns 101 then. First 100 cells are normal. And scroll indicators still make sense.
If the 101st cell is displayed, you display a progress indicator like UIActivityIndicator in the cell, and initiate the process to load next 100 rows. When the data arrives, you either reload the whole table with UITableView reloadData, or you insert new cells individually with UITableView insertRowsAtIndexPaths:withRowAnimation:.
So, you just keep infinitely appending to the table. This is easier than trying to keep the table as a "window" that is always at N cells/rows and unload from the beginning of the table. If you need this sort of window, you may reconsider if this is really the best way to interact with your data.
I just finished implementing a variation of this.
Lets assume you have a 100 rows and 8 of them fit in a screen. Start by adding the last 8 rows first, then the 100 rows normally and then the first 8 rows at the end. Recenter the tableview by using setContentOffset to the first row of the 100.
When the user scrolls below the 100th one he will see rows 1-8, you can detect this in the viewDidScroll delegate function and recenter the scroll back to the first record (ensure u set the animated flag to NO when you do this). Similarly when the user scrolls past 1, he/she will see the last 8 records, you can again recenter the scroll view to show the last 8 records in the 100 row section.
This actually implements circular scrolling and the user can keep scrolling in both directions.
I haven't tried this but here's an idea. Let's say N = 100, since you have 100 rows. What we'll do is tell the tableview we have 200 rows, but we'll keep the user scrolling in the range 50-150.
Tell (the tableview) that you have double that number of rows, so for numberOfRows, return 2*N. In cellForRowAtIndexPath, always return the cell corresponding to (row % N).
Now init the table scrolled to row N (instead of 0) so that the user can scroll in either direction.
As the user scrolls up the tableview will eventually ask for row N+(N/2), when that happens send a scrollToRowAtIndexPath:atScrollPositionanimated: to scroll to the row-N. That will reposition the table back by N (ie: from 150 to 50), same cell but we'll never run off the end of the table this way.
Do the same when the tableview asks for cell at row N-(N/2) for scrolling up.
You'll have to deal with the special case where N cells fits in the view with extra room, ie: what if N were 1?
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.