Is the separator part of a UITableViewCell? - ios

I've written a custom GridView (UIScrollView Subclass) and decided to use a standard UITableViewCells as Cells because they have all the functionality I need.
Everything works fine but I've noticed that sometimes I have a thin gray line at the top of a cell.
I actually thought that the UITableView handles the separators of the cells and they actually don't belong to a UITableViewCell but it seems to be that I'm wrong.
In fact I want to get rid of them but I don't know how.
Does anyone has experience with this or is the only way to make a custom cell class with the features of a UITableViewCell?
Here's a screenshot:

After some experimentation, try & error and subview iterating/checking I can tell that the separator is definitely part of an UITableViewCell. Thanks to #Valeriy Van.
The answer to my first question simply is: YES, the separator is
part of a UITableViewCell.
But it is strictly handled by the UITableView and is empty if it is not populated by the TableView.
Now to the lines in the image above:
These are not separators. They just look like that, such a damn coincidence.
They happen when the Graphics tries to draw something at subpixel-precise coordinates.
This can happen, when the coordinates and/or sizes (the frame) of an visible object (the layer of the view) are between two integer values and the GL tries to render it properly.
Due the "overprecise" frame, the Gl tries to interpolate to integer frames or just not round properly.
So it can happen, like in the picture above, that the first line is stretched or the y-coordinate wrong by one pixel.
This can also happen in a UITableView if you return "overprecise" values for cell-heights.

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Edited:
Below is a line from UITableView.h
#property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; // default is UITableViewCellSeparatorStyleSingleLine
UITableViewCell, UITableView, UITableViewController designed to work together. And your problem is you try to use UITableViewCell separately. Logically, cells separator is not characteristic of a cell, it's characteristic of a table. That's why separatorStyle is property of UITableView, not UITableViewCell. Well, different approaches are possible, and Apple had chosen this. Look into UITableView.h for how many times 'separator' substring occurs there. Have a look into UITableViewCell.h: UITableViewCell has reference to UITableView. And UITableViewCell coded accurately enough not to crash if _tableView is nil. I think, if _tableView is nil, cell applies same defaults and draws itself with default separator.
What can you do to get rid of separator in you case? If you can't use UITableView in way it was designed, cheat in some way. Try to feed cell with some dumb tableview with properties you need.

Related

How to add UITableView cell pagination with “peeking”?

I want to add something like what’s in the app store app:
As you can see, it isn’t just one cell that is paged, but you can also see the two edges of the cells from the left and right as well. I tried to implement this in my app by making each cell slightly smaller than the size of the collectionView, and then enabled isPagingEnabled, but when I flipped from cell to cell, it didn’t page from cell to cell, rather the width of the entire UICollectionView every time. This ended up in an unwanted effect where each page turned resulted in an increasing offset where cells were shifting further and further off the screen.
I researched a bit on this and implemented the targetContentOffset(forProposedContentOffset... but it was never called and didn’t work.
Preferably, I would want to keep the smooth behavior of the built-in pagination and just be able to tweak it.
Thanks!
Instead of implementing the peeking behavior yourself delegate it to a third party library iCourasel. Implementing it yourself you will ended up a UICollectionView inside of UITableViewCell. As of your requirement iCarouselTypeLinear suits your requirement.

Is subclassing UIView the right way to go about an unknown number of these custom objects?

I have a screen in my app that pulls an unknown sized array from my user object, and must display an object for each item in the array that consists of a UIImageView for my background image, a UISwitch to activate/deactivate that particular item, a label with the name of the item, and a label that corresponds to the name of the item.
These are displayed in a UIScrollView, since there are nearly always going to be more items than will fit on the screen, especially on the iPhone 4.
I had initially quickly thrown together a static screen that consisted of 20 of these objects, but have found that I will often be exceeding that number of items in the array, and need to convert to a dynamic solution.
I have not used subclassing at all yet, and want to be sure that this is the proper solution before I dive into the implementation. Adding one subclass to my scrollView sounds a lot cleaner than adding each of those components individually and figuring out the spacing for various screens.
If this is the proper solution, are there any resources you could point me to to learn about how to properly do this subclassing? I.e., how to add a constructor for the objects where I can add the contents of the labels and state of the switch, and programmatically add constraints. I've only used constraints from the storyboard.
If you do not think that subclassing UIView would be the proper way to do this, what other solution would you suggest? I'm admittedly a novice when it comes to iOS development, so I apologize that this post seeks advice on where to start rather than help fixing a specific part of an implementation.
You should use UITableView or UICollectionView for that, and subclass UITableViewCell or UICollectionViewCell to create your custom view hierarchy.
UITableView and UICollectionView will handle reusing of cells which will help in memory management.
Update:
as you want to use UITableView so to add spacing between cells set number of sections to number of elements and then add header for section and set its height you can do that by implementing UITableViewDelegates visit below links for detail about UITableView and UITableViewDelegates
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/

How do I make a UITableViewCell separator show up?

I'm having a similar problem to this question: UITableViewCell separator not showing up.
However, the proposed solutions don't work for me. Specifically, I've checked the following:
My UITableViewCell subclass does not override drawRect:.
I don't have a custom UITableView and, therefore, I'm not overriding layoutSubviews.
In Interface Builder, I have the Separator attribute of the Table View set to "Single Line".
I've tried programmatically setting tableView.separatorStyle to UITableViewCellSeparatorStyleSingleLine.
But still the separator line will now show up… Does anyone know of something else that could cause this behavior or have any ideas for how I might track down what is hiding/removing the separator line?
EDIT:
Well I feel silly… In trying to create the screenshot requested by valheru, I noticed that the lines ARE there, but are almost exactly the same color as the background, so I couldn't see them until the screenshot was blown up. I had suspected earlier that the separator color might be the issue, and I tried testing for that by setting the separator color to white (my background is almost black), but it turns out that there was some copy/pasted code that was programmatically resetting the color to the almost-invisible color.
Thanks valheru for leading me to the answer. :-)
Check the height of your table view's row and the height of the cell. Make sure that the row's height is not less than the cell's height.

UITableView row count like iOS Mail app

How do you add counts inside of a UITableView UITableViewCell like the iOS Mail app?
In addition to DDBadgeViewCell (mentioned by #micpringle), there's also TDBadgedCell.
I tried out both and found TDBadgedCell to suit my needs more, as it puts the badges over the cell's text rather than under it, meaning the badges are visible even for cells with long texts.
The project also seems to be (currently, at least) more active than DDBadgeViewCell. (That being said, there seems to be a bug in the non-ARC version of TDBadgedCell.)
Create a custom UITableViewCell, position the labels where you want them (title, subtitle, count, whatever you need). I highly recommend Matt Gallaghers custom UITableView code - it takes a lot of the headaches out of dealing with custom rows. You'll have to follow Matt Gallaghers steps for customizing the cell.
To get the appearance of the count label as close as possible to your example (mail.app), you'll have to set the UILabel backgroundColor to gray (or whatever color you want it to be), textColor to white, and layer.cornerRadius to something equal to half the height of the label (if label is 20 high, cornerRadius should be 10). This will result in a UILabel with white text, gray background, round corners. Note - this isn't the most efficient method of doing this, but Apple hasn't put up the WWDC session video where they explain the performant method better (I missed that session).
The easiest solution would be to set an UILabel as accessoryView or using a custom UITableViewCell subclass which could be designed using IB.
I'd recommend creating a simple rounded UIView and a UILabel as a subview in it. I'd probably create a UITableViewCell subclass to manage the content.
Definitively the most easy way would be using a ready-to-use class like TDBadgedCell

Add multiple lines to detailTextLabel in UITableViewCell

I'm using a UITableView with cell style of UITableViewCellStyleValue1.
I'm want to have multiple lines in the detailTextLabel, is that possible? Or do I have to make a custom cell?
You can set cell.detailTextLabel.numberOfLines = 2 to get 2 lines in there. However, I doubt UITableViewCell will lay out the labels as you expect in that case. You may want to subclass UITableViewCell and override -layoutSubviews to position the labels how you want. You can call [super layoutSubviews] and then just tweak the positions of the labels. You'll probably want to use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] to calculate the correct size for the detail text label.
Alternatively, instead of subclassing UITableViewCell, you could try doing the tweaks in -tableView:willDisplayCell:forRowAtIndexPath:, though if the cell ever decides it needs to re-layout, then your tweaks will be erased. I recommend you go with the subclassing approach.
Edit: BTW, with the subclass approach, all you have to do is change [UITableViewCell alloc] to [MyTableViewCellSubclass alloc]. Since you're not introducing new methods or properties, the variable can still remain typed as a UITableViewCell and you won't have to change any other code.
The detailTextLabel is a UILabel, the word wrapping rules apply just like with a label you create.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/lineBreakMode
EDIT: Just thought, you'll also need to change the height of the cell if the text gets beyond some threshold of lines. probably 2 or 3 is okay.
At least on iOS 11 it works great just adding this line.
cell.detailTextLabel.numberOfLines = 2;
On SWIFT 4.0 and above:
cell.detailTextLabel?.numberOfLines = 0
Unless you never want to have more than 2 lines, do not set numberOfLines to 2 as advices in the comments because it's not an efficient way to code. It's not dynamic. The number of lines might be different base on your screen size, accessibility settings etc. Setting numberOfLines to 0, let the OS use as many lines necessary to display properly your text.

Resources