How to change the background color of a table view cell after it was moved? - iphone

I have a table view with 9 rows at minimum. The background colors of the cells will depend of the cell position. If it is bellow to 3 or below to 6 or below to 9, the background color changes (see the next image).
The problem is that this table view can be reordered and I have to maintain the same color pattern. Example: if i change the 4th row to the 3th position, its background color must change from yellow to red.
To get the different colors, I used the method - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath, but this method is called only when the cell will be displayed and isn't good for reordering issues (the cell is already displayed and the color don't change).
I already tried to create a method to change the background color that is called just after the row was moved, but the color didn't changed (it seems that changing the background color with cell.backgroundColor property just works in - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath). :(
Any ideas how I can do that?
Thanks in advance.

Adjust the position in your data source however you are doing, then tell your data source to recalculate the colours for each item based on their current position. Then reload your table view. It should "just work".

Related

Letting big shadows bleed between UITableView cells

I've created a UITableViewCell with a textfield in it, which has a large diffused shadow underneath it. I've also set it up so that these big shadows can blend under its surrounding cells.
NB. Shadow has been HEAVILY emphasised here to show the problem.
The problem is that I want the shadow from my EMAIL textfield to blend underneath the password cell. i.e. I want both textfields to have a white background, but underneath them both I have the shadow. Something like this:
The reason this is a problem is because the UITableView is being rendered with upper cells "above" the lower ones. So any shadow from the cell at row 1 will bleed on top of the cells in rows 2, 3, 4 etc.
I was wondering if perhaps there is a way to change this rendering, and instead reverse the rendering so that a cell in row 4 is actually ABOVE rows 3, 2 and 1.
Or perhaps there is a way of setting the Password textfield so that no shadows are rendered on top of it?
Any help, very much appreciated!
Thanks to #Amr for pointing me in the direction of the cell.layer.zPosition.
The higher the value this is, the "closer" to the viewer the layer sits, so as long as each row has a higher value, it will appear "above" the shadows from any cells around it.
Since I use multiple sections in my code, I used this line to assign each cell above the previous one.
cell.layer.zPosition = CGFloat(indexPath.section) * 1000 + CGFloat(indexPath.row)

Show a label over the limits of a UITableViewCell

I'd like to use a UItableView to show a day Calendar. Each row corresponds to one hour. I need to show the hour between 2 cell of my tableview.
Like this :
(source: free.fr)
And this is my UITableViewCell :
(source: free.fr)
In the first screenshot, it works perfectly but if I scroll down then scroll up, my time label is cut like this :
(source: free.fr)
Have you any tips to figure out this problem using a tableView ?
The way you lay out your cell now is fragile, because the order of painting the cells on screen matters a lot. Try moving the content up so that your buttons are flush with the top of the cell, and the time label fits into the cell entirely. Add a thin header view to your table to make the top cell appear normal. Keeping the content of a cell entirely within its bounds should help you maintain reasonable scrolling speeds.
EDIT : You could also put a second clipped label at the top of your cell, and make its content identical to that of the label in the prior row. You would need to take special care to hide that label in the top row, but otherwise this should make your table immune to changes in the rendering order of its cells.
Make the background color of your cell clear. As you scroll up the z ordering of your cells get reversed and the lower cells overlap the higher ones causing this clipping.

UITableView with text that is both right-aligned and indented

I wanted to make a UITableView with text that is both right-aligned and indented as depicted in the image below:
Unfortunately, I can not do this by writing :-
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.indentationLevel = 10; // or even -10
Can this be done using UITableView's properties? If not, the only way I could think of is using [myString drawInRect:withFont:]; but I would like to go through methods based on alignment and indentation before getting into that [I have already written code for that :-) ], so other work-arounds are welcome!
Additional info: The indentation varies with accelerometer values so I can not have hard-coded Label frame positions. I've uploaded sample code at github in which I've used only the alignment and indentation info so far, so continuing to use that would make this easier.
Subclass UITableViewCell and you can position the frame of the text label however you like. In the if statement where you dequeueReusableCellWithIdentifier: where a reusable cell doesn't exist, just modify the frame and then set the label to use the adjusted frame with setFrame. The autoresizing mask should remain the same.
You could always create your own custom cells and place a UITextLabel in the cell, and make the UITextLabel's alignment right aligned.
Also set the autoresizing mask of the UITextLabel to the right so the indentation distance stays the same no matter the orientation.

UITableview section issue

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.

Cell Separator Style

How can we set Separator style at cell level. i.e. each cell will be having different separator style?
I don't think you can set the cell separator style per cell. You might try setting the table view separator to UITableViewCellSeparatorStyleNone and then draw a custom "separator" yourself when you render out each cell.
Edit: How to do it depends a lot on your current code, what type of table, whether you are using a custom table cell, what exactly you mean by "different separator style", etc.
I have not tried this, but one option I can think of off the top of my head would be to use the UITableViewCell.backgroundView property. You could add a subview with a different color that is only a few pixels high along the bottom or you could create a UIImageView that fills the backgroundView and set the image to achieve the "different" separator.