1

I have set my tableView's background color, and I want there is space(10px) around each cell show that color.

e.g.(ignore the red line)

enter image description here

What I am doing now is make the cell embed a smaller view, and set the cell's background to be clear. But it is annoying to create such embed view again and again.

So, I am seeking for a better solution, I believe it would be good if i can increase separator's height. But the old answers did not solve my problem.

(How to increase the UITableView separator height?)

EDIT:

However, if I add my own separator in cell. I may not able to set the cell having round corner

Community
  • 1
  • 1

2 Answers2

2
  1. Set the tableView's separatorStyle to .None.
  2. Create a custom UITableViewCell subclass.
  3. In the storyboard (or xib if you created one for the custom cell), add a subview that's inset the amount that you want padded on all four sides.
  4. If you want rounded views, do that on the custom cell's subview (e.g. subview.layer.cornerRadius = 10.0, subview.layer.masksToBounds = true).
  5. Set the custom cell's ContentView to have a clear background.
  6. Set the table view's background to that lovely shade of blue.
  7. Set the cell subview's background color to white.

You don't have to use the cell repeatedly in InterfaceBuilder. Just give the cell an ID, and in your implementation of the table view's data source, dequeue a cell instance using that ID and set the values of the labels inside it.

NRitH
  • 13,441
  • 4
  • 41
  • 44
  • thanks for your long answer! However, I do cell in different tableView has different content. So, I cannot reuse cells like this – Justus Jianxing Zhang Aug 24 '15 at 21:28
  • All of the cells have the same basic content: a white background view, a blue circle on the left, a description label, a date label, and whatever that "9/12" on the right means. If each of those labels and views has an outlet to it, then when you dequeue a cell for each row, you can set the values of that cell's fields. – NRitH Aug 25 '15 at 00:35
  • I mean, I will need to create cells that do not have images, some cells may only contains images in different tableViews. BTW, i believe it is not suitable to "dequeueReusableCellWithIdentifier" in other tableView – Justus Jianxing Zhang Aug 25 '15 at 01:04
  • You create a prototype cell for each style you'll display. – NRitH Aug 25 '15 at 01:21
  • it seems "prototype cell" can only be used in one table view, app will crash if you try to create "prototype cell" defined in other tableView – Justus Jianxing Zhang Aug 25 '15 at 01:51
  • 1
    Kind of. Your cell type has to be *registered* with each table it'll be used in. When you add prototype cells in a xib or storyboard, they get registered automatically. But if you want a cell class to be used in more than one table, you have a couple of choices: you can create a xib and refer to it in multiple tables, or you can call `registerNib: forCellReuseIdentifier:" in the code for both tables' data sources. [This](http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files) is a good summary of how you can do this. – NRitH Aug 25 '15 at 01:56
  • Thanks a lot !! I prefer create cell to separate them though – Justus Jianxing Zhang Aug 25 '15 at 01:58
1

Have you thought of just making the cell height taller and putting your own separator on the bottom of your custom cells? Nice table layout by the way.

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73