Cell Separator Style - iphone

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.

Related

Automatic Cell Height Without Custom Cell?

I would like the Subtitle-style cells in my iOS 8 table view to resize automatically in height to allow subtitle content of any number of lines. And, I would like to accomplish this without using custom cells, if possible. That is, I would like to use the "canned" prototype cells that are available when dragging a new TableViewController to the story board.
I have found that this is easy to do with Basic-style cells: that is, prototype cells with Style set to "Basic" so that there is just one label in the cell -- "Title", by default. This is the label that is accessible by cell.textLabel!.text in code. If I set that textLabel's number of lines to 0, and add the two lines below to viewDidLoad(), then the cell heights resize automatically:
tableView.estimatedRowHeight = 144.0
tableView.rowHeight = UITableViewAutomaticDimension
All without having to define a custom cell or muck around with auto-layout or constraints. (In fact, I find it's not possible to add constraints to a canned prototype cell.)
But if I change the cell Style to "Subtitle," so that there are two labels in the cell -- textLabel and detailTextLabel in code -- then the cells do not automatically resize in height, even after setting both (or either) line numbers to 0 and playing around with different estimatedRowHeight values.
Am I missing something, or is it not possible to automatically resize cell height without a custom cell?
Thank you!
or is it not possible to automatically resize cell height without a custom cell?
Yes, it is not possible. And the reason is the reason that you gave. The automatic-resizing-cell-height feature depends upon internal constraints. But, as you have rightly said:
it's not possible to add constraints to a canned prototype cell.
However, note that you can accomplish what you want without supplying a cell subclass (though personally I like supplying one). You can just design your cell, constraints and all, right there in the prototype cell — the Custom prototype cell.

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.

Add separator in grouped tableview cell

I have table cell with 1 textfield & 1 uiswitch placed in single cell. I have table cell of height 100. How can I add separator to this cell so that there is distinction between textfield & uiswitch?
Assuming that you are adding subviews to your cell, you would need to create a custom view with a switch and a separator (a graphic or something else) to add to your cell as a subview.
Question: if you are adding a switch and a textfield, shouldn't they be in two separate cells anyway? That way you would get a natural horizontal divider.

UITableView cell text is cut off

I am having a problem with the UITableview cell cutting off strings whose characters are more than 12 chars. Any ideas why this would occur? I have not made a custom cell at all. I cannot find any solution to this problem through a Google search. Any ideas?
You should be able to set the label properties to re-size the font based on the label's contents using adjustsFontSizeToFitWidth. This will essentially decrease the font size to make the text fit all on one line.
Your cell likely contains a label, which in turn is set to given bounds. What you need to do is ensure that your label is the same size as your longest string, or bigger than it.

Shifting UITableViewCell Content to the right

Is there a simple way to shift cell content to the right? Like a horizontal alignment or some sort of offset?
I'd rather not
include a blank cell.imageView.image, or
add spaces to each cell.textLabel.text.
Any ideas please. Thanks.
Create a UIView that contains subviews positioned where you want, i.e. set their frames. Then set the cell's contentView property to this parent view.