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];
Related
1Hi in my project there is a requirement to show table row selection(blue color) only half of the row.Is there any way to do this.
The selected cell should be in blue color and remaining all are in white. Please help me to achive this also.I dont want to reload table because many images are there loading from server.If reload again again it will slow down performance.
[Please see the image after selection the cell should appear like this]
enter image description here
You can set selectedBackgroundView property of UITableViewCell with a view whose half is blue.
In didSelectRowAtIndexpath get cell ..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// add view as per your according to need in cell
UIView * myCellAccessoryView = [UIview alloc] init];
// set frame of myCellAccessoryView as per your need ..
myCellAccessoryView.backGroundColor = // set color as you need .
// and add in cell
[cell.contentView addSubView :myCellAccessoryView];
}
hope it helps you ..
You can use this
UIView *backView = [[UIView alloc]init];
backView.frame = CGRectMake(0, 0, cell.frame.size.width/2, cell.frame.size.height);
backView.backgroundColor =[UIColor blueColor];
[cell addSubview:backView];
This will create a blue color background view.
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.
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.
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.
I have a full list that populates a UITableView. This I want to background the one of the cells with a different color and it works initially, but for some reason when I start scrolling the table up and down, it starts drawing more cells with the green background.
Please note that there is always one detailCell.detailTimeLabel.text that's equal to currentTime.
The code I have is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... SOME MORE CODE HERE ...
if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
detailCell.backgroundView = backgroundView;
for (UIView* view in detailCell.contentView.subviews)
{
view.backgroundColor = [UIColor clearColor];
}
}
}
Which can be the problem?
Your trying to store data in a tableviewcell. You can't do this because the cells are constantly reused. The background you see repeated occurs because its the same cell being displayed over and over again with different text.
When you dequeue the cell, you need to reset it to blank and wipe out all the previous data. Then you should set the background color only if the data you are putting into the cell has the current time.
As a general rule, you should never refer to data in the cells. If you need to know what is in a particular cell, look at the data at the indexpath within the datamodel itself.