UITableView with scrolling background pattern - ios

I have a UITableView with the backgroundColor property set like so:
tableview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"IMAGE"]];
This gives the tableview a background, that scrolls, which is exactly what I want.
The problem is, when I add cells to the tableview, in each cell the background pattern of the tableview starts new / starts over:
In the above image, you can see the problem in the red circled areas.
For the tableview cells, I have set all backgroundColors to clearColor for
backgroundView
textLabel
detailedTextlabel
contentView
I have also tried to set
cell.backgroundView = [UIView new];
cell.backgroundView.backgroundColor = [UIColor clearColor];
and the same for the contentView, too.
Additional Note:
Setting the backgroundView of the tableview does not help, since then the background does NOT scroll anymore!

Just ran into this issue and found this fixes the cell having it's own copy of the backgroundColor
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor:[UIColor clearColor]];
}

Here is what I would do:
1) Create a view controller and set the view background color to your pattern image.
2) Add your table view controller as a child view controller to the newly created view controller. This can be done in code or if you are using iOS 6 and storyboards, with an embed segue. If you are doing it in code you may need to set the frame of the table view controller manually.
3) Set the background color of your table view controller to [UIColor clearColor]
I had a similar issue and this is what worked for me.

Related

Transparent Section in TableViewController

In my TableView I use static cells. I take one section and in this section there are five cells(static cells). Now I want to make the whole section or these five cells transparent. That means I can see the Table View Controller background through the cells. How can I do that?
I have gone through many solutions. But not solved.
Edit:
May be many of you are not clear about my question. I want the cell like the below image. it is downloaded image and there will be section which is not available in this image. I am just using this image to clear the question. Look at the image the cell background is transparent.
To achieve this in the most simple way possible:
In storyboard select the static cells you want to appear transparent and select the background colour of 'Group table view background colour' or R235,G235,B241.
Then move the separator (the line in between) by moving it off screen by setting the below settings.
If you still want the line ignore the above or if you want to change its colour use
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = //ADD COLOR HERE
}
Then you can add content to these cells as you would normally as I have added the label and UIImage View above.
!!However, the less simple but best way to achieve this is by creating custom cells and using dynamic prototypes. You can find many tutorials on how to do this online.
you have to clear color to contentview of cell,also to the tableview cell and now i think you will be able to see the background and check now
just add below code for tableview
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
Override your TableViewCell's awakeFromNib to set it's own background color and it's contentView's background color to clearColor:
class MyTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = .clear
self.contentView.backgroundColor = .clear
}
}
You need to set tableView with clear background color. Also tableView cell and contentView backGround color as Clear color.
For this add this Line in viewDidLoad Methods.
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];
Add this line in cellForRowAtIndexPath Method.
cell.backgroundColor=[UIColor clearColor];
cell.contentView.backgroundColor=[UIColor clearColor];

UITableViewCell will not work with UIColor colorWithRed [duplicate]

I have created a bunch of cells which I reuse in a table view. All of those cells just have different UILabels inside them and some UIImageViews (nothing covers the complete cell).
Setting the background color in IB has no effect (always white or transparent, can't say which one of them). But if I press Command-R (simulate interface) the cell has the correct background color in the simulator.
I tried to set it in tableView:cellForRowAtIndexPath: but it doesn't work like I would think either.
This does the trick:
cell.contentView.backgroundColor = [UIColor redColor];
but these have no effect (even if I set the cell.contentView.backgroundColor to clearColor):
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
I set all the layout/font/background stuff in IB. Any idea why this isn't working in this case?
Why do I need to modify the contentView's backgroundColor and not the backgroundView's?
It seems to be a common issue. Could somebody please point me in the right direction to (finally) understand how background colors are handled within a table view cell.
To change the background color of the cell, you have to do it in the willDisplayCell:forRowAtIndexPath: method. This is mentioned in the UITableViewCell documentation near the bottom of the Overview. So you need:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
The contentView is just the recommended subview to put custom controls in so that the cell gets layed out properly when table editing is going on.
There's an easier solution: When you create a UITableViewCell in Interface Builder, simply drag and drop an additional UIView so that it covers the entire UITableViewCell you're creating within IB. Place your additional UI elements on top of this additional UIView. You can set this extra UIView to whatever background you like.
There are quite a few answers on StackOverflow that suggest you change the background color of your UITableViewCell by using a line of code like this one:
cell.contentView.backgroundColor = [UIColor redColor];
If you add a drop shadow to your cell by adjusting the contentView's frame size slightly, you may find that this line will change both the background color of your cell and the color of your drop shadow area as well.

How to make UITableView looks like this

is it possible to make a distance between cells like that in standard UITableView? There is no options to make separator bigger. Also this distance from right and left.
you can do this by set your cell background to whatever background you want (let's say gray in this case) and in the cell, add a white uiview with left, right, and bottom margin like this:
Then go to your table view, set the separator as none
and one last step, set the background of your table view to the same gray background as your cell.
Then you don't have to do anything particularly in your code besides simply initial your table cell based on the custom nib file (i assume you want to do this anyway).
If you prefer to construct your cell in codes, you can use the same logic.
in method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = [UIColor whiteColor];
// This will create a line of 3 pixels that will be separating the cells
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,3)];
separator.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separator];
// and if you want the border do left and right add:
UIView *separatorRx = [[UIView alloc] initWithFrame:CGRectMake(318,0,2,cell.frame.size.height)];
separatorRx.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separatorRx];
UIView *separatorSx = [[UIView alloc] initWithFrame:CGRectMake(0,0,2,cell.frame.size.height)];
separatorSx.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview: separatorSx];
return cell;
}
One way would be to set the backgroundView of the UITableViewCell to an UIImageView containing this white box on that gray background
You have to make a subclass of UITableViewCell. In your own subclass you may make everything you dream about! So, you need to add UIView with whiteColor background and margins as subview on your custom UITableViewCell.
There is not padding option in UITableView. You can either add a UIView in your UITableViewCell which will contains all the cell subviews, and change it's frame to create a padding right in the cell.
I suggest you to use UICollectionView instead, you can easily set the space between cells using a layout. If the cell is smaller than the actual collection View, then it's automatically centered.

Changing background color of selected UITableViewCells in iOS5

My app displays grouped static UITableViews in different screens. Is there anyway of using the appearance proxies
[UITableView appearance]
or
[UITableViewCell appearance]
to customise the background color for the selected cells? Basically I would need to modify
cell.selectedBackgroundView.backgroundColor
for each cell in my app,but I cannot find the right property to be set in the proxy object.
BTW I have tried also the normal approach (being a group static table):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];
}
but it doesn't work. Any suggestion?
You need to provide a view to selectedBackgroundView. There isn't one already there. Try:
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor] ;
Also, a better place to put this code (at least the first line) is in -tableView:cellForRowAtIndexPath: when the cell is created. Otherwise you will be creating a new background view every time the cell is displayed.

Why doesn't UITableViewCell background color work (set in interface builder)?

Why doesn't UITableViewCell background color work (set in interface builder)?
I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?
It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:
cell.contentView.backgroundColor = [UIColor redColor];
What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];
Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.
Recommended reading:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
and then:
http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html
You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.
This Works !!
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
Make sure before defining a cell color your backgroundView is null
cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];
This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.
Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.
It works perfectly, if you pick the tableview and set background to for example red the whole table will be red.
maybe you do not linked the table or something
hope it helped
(try this)
Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.
Y not, use setBackgroundColor
self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
You can partially do this in Interface Builder (probably added to Xcode recently):
Expand the cell in the Storyboard's Document Outline and select "Content View"
Set Content View background color in the Attributes Inspector
Select each content item in the cell and set their background colors
to ClearColor
Still don't see a way to set the accessory background though.
I did get this issue also.
In IB you get the ability to change the background color of the cell when choosing cell. This is NOT correct. You should go to the document outline on the left of the storyboard and choose the content view inside the cell. Change the background of the content view. You should now get correct cell color.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}
We need to change the color in this method.
Try this it works for me:
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"Cream.jpg"]];
}

Resources