TableView Cell alpha trouble - ios

In my project I have tableview controller with Static Cells.
I need to set alpha for cells.
I do it with this code:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setAlpha:0.25f];
}
But my problem is that [cell setAlpha: 0.25f] also affects the textLabel.
Please, help, how can I fix it?

You can set the background colour of your cell:
cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.25.f];
This will set your cell background colour to white, and its alpha to 0.25.
Hope it helps.

The textLabel is a subview of your cell, so it's normal that if you set the cell alpha, its subviews are impacted to. The way you can solved this is by making a custom cell where the text label won't be a subview of your background view (the one you want to set an alpha on) so it's not impacted by the alpha.

Let's try:
// i do it in cellForRowAtIndexPath
cell.contentView.alpha = 0;

Related

change the background color of customize tableViewCell

I want to change my cell's background color
I see other question but they are not correspond my situation
I want to do a notification tableView
for example,If user have read cell, the cell's background color will change to white
If not, the color is yellow
At beginning
I set color in
-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(read[[indexPath row]])
[cell.backView setBckgroundColor:[UIColor whiteColor];
else
[cell.backView setBckgroundColor:[UIColor redColor];
}
It works, and then i want to change color in
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
read[[indexPath row]] = yes;
[cell.backView setBckgroundColor:[UIColor whiteColor];
cell.name.text = #"test";
}
it works too
BUT if i selection other cell, it change to orignial color
It seems it only can change ONE cell's color at same time
No matter I use
cell.backgroundColor
cell.contentView.backgroundColor
cell.backView
it get the same result,can anyone help me
edit 4/20 20:13
I set read in
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
sorry for misdirection,i update code
and after i selection other cell, i don't call [tableView reload]
so i don't think this is the reason
By the way, everything(e.g label) can change but background color
and if i select cell , it jump to other screen by navigation
Ans
conclusion first
tableView: cellForRowAtIndexPath: is well
and
tableView: willDisplayCell: is well too
both of them can change background color
but they execute when need to draw new cell or reload tableView
I still confuse why I can change label in didselectionRowAtIndexPath
but i can't change color
Change color using tableView: willDisplayCell: method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){ //do your stuff.
[cell.backView setBckgroundColor:[UIColor redColor];
} else {
[cell.backView setBckgroundColor:[UIColor whiteColor];
}
}
I think you want to set your variable "read" to YES in
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
and I guess you use only one variable to record the states just because you simplified it for posting here, otherwise you might want to record the state of each cell with separate variables.
You need to update the "read" property of the object at the index of your cell.
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
[cell.backView setBckgroundColor:[UIColor whiteColor];
currentObjectForThisCell.read = YES;
}
then:
-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if(currentObjectForThisCell.read)
[cell.backView setBckgroundColor:[UIColor whiteColor];
else
[cell.backView setBckgroundColor:[UIColor redColor];
}
In your edited code, when you select the cell, you set the read flag to YES and change the color to white, that's fine, but in your cellForRowAtIndexPath: you set cells with the read flag YES to red, and NO to white, which is opposite to the behaviour in your didSelectRowAtIndexPath: method.
Try to get the Selected cell in the didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//set read property for the seleted index
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
}
Have you called reloadData for the tableView
Set read property in the "didSelectRowAtIndexPath" and reload the tableView

how to create different view for last visible cell of tableview

I want to show last visible cell with different background color (say green).
I am using this code, It works good until I scroll up/down. When I scroll up/down it gives many cell with green background. (I guess this happening due to "dequeueReusableCellWithIdentifier").
Can anyone help me out where I am doing wrong or what is the best way of doing this.
NSArray* indexPaths = [tableView indexPathsForVisibleRows];
if (indexPath.row==[indexPaths count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
Thanks in advance.
Firstly, you shouldn't be allocating anything in cellForRowAtIndexPath - it is bad for performance. You should only be configuring views and controls that already exist for every cell. Because cells are reused, your cell that you add a background view to will get re-used too... and its background view.
In your case, you probably just want to set:
if (lastCell)
cell.contentView.backgroundColor = [UIColor greenColor];
else
cell.contentView.backgroundColor = [UIColor clearColor]; //or whatever color
contentView is already a view every UITableViewCell has for free, so just use that for background color. Apple probably intended it to be used for this case, amongst others.
You should nil the background view before the test.
cell.backgroundView = nil;
if (indexPath.row==[indexPaths count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==[yourtableviewarray count]-1)
{
UIView *v=[[UIView alloc]initWithFrame:cell.bounds];
v.backgroundColor=[UIColor greenColor];
cell.backgroundView=v;
}
}

Set specific Frame for Background Color by Cell click

i have the following Custom Cell. On the left is a blue label with an image on it, and on the right side a label with some text. No i want that the cell gets highlighted when i click on it, BUT only the white part behind the label should get highlighted, not the whole cell with the blue label and the image, any ideas?
I tried something like this:
categorieCell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 0, categorieCell.frame.size.width, categorieCell.frame.size.width)];
categorieCell.selectedBackgroundView.backgroundColor = [UIColor redColor];
If you don't want the cell to be highlighted as a result of the selection, just set the cell's selectionStyle property to UITableViewCellSelectionStyleNone. That will disable overall cell's highlight when it gets selected.
In tableView cellForRowAtIndexPath method apply the following code:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
After that you can change cell background upon the cell gets selected.
In tableView didSelectRowAtIndexPath method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *categorieCell = [tableView cellForRowAtIndexPath:indexPath];
categorieCell.backgroundColor = [UIColor redColor];
}
Don't forget to change cell background when it gets deselected in willDeselectRowAtIndexPath:

ios 6 UITableViewCell delete button has background color

i have custom UITableViewCell with background pattern
using this method
- (void)tableView:(UITableView *)tableView willDisplayCell:(homeCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
}
and the result was good,
ImageLink: link
but when i swipe to delete, the delete button comes with background color overriding the content of the cell
ImageLink: link
i followed some solutions that i found here in stackoverflow, but none solved my problem
i've tried clearing background color of the delete button using this code
- (void)tableView:(UITableView *)tableView willDisplayCell:(homeCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
// create uiview
UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
myBackgroundView.backgroundColor = [UIColor clearColor];
// assign the uiview
cell.editingAccessoryView = myBackgroundView;
cell.backgroundView = myBackgroundView;
// my background patter
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"CellBG.png"]];
}
but the result was not good, the whole cell background was with no background color
ImageLink: link
how to display delete button with no background color? please help
some of the solutions i tried:
link 1
link 2
link 3
The problem is not from the delete button background, the problem is that when the delete button is shown, your UITableViewCell content view size is decreased. By adding the labels from IB they are added to the contentView.
What I did in my other projects (I didn't use autolayout) I set the springs and struts so that the labels size will be re sized when the UITableView cell will be re sized.

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