I'm having an issue that's appeared since upgrading to iOS7 in which when I try to change the background colour of a specific tableview cell, it doesn't colour the correct cells (usually the specified ones in addition to other ones). As you can see from my code below, I define the type that I want to be highlighted and then change the colour. It worked perfectly prior to the iOS upgrade so I'm not exactly sure what change has been made that's causing this:
Quick edit: also, when I scroll down the tableview and then back up, it colours more cells that weren't coloured when the tableview controller first loads (if that helps at all).
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
cell.backgroundColor = [UIColor colorWithRed:0.81 green:0.91 blue:0.81 alpha:1.0];
}
}
I think doing cell customization in tableView: cellForRowAtIndexPath: method is better. In this method,
if ([type isEqualToString:#"ace"])
{
cell.backgroundColor = [UIColor aceColor];
}
else // this else is important. If you add this, scrolling works fine.
{
cell.backgroundColor = [UIColor otherCellColor];
}
You likely have a single re-usable cell style. Consider having a re-usable cell style for your aces, and one for all others. Set the background color in cellForRowAtIndexPath, not willDisplayCell.
pseudo-code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
NSString* type=[[self.HandPointer.player_hand objectAtIndex:indexPath.row]cardType];
if ([type isEqualToString:#"ace"]){
{
// load a cell with the background color desired
cell =
cell.backgroundColor =
.
.
.
return (cell);
}
// else a normal cell
cell =
.
.
.
}
Related
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
I want to show UITableViewCell selection style "Gray" without selecting the cell. How can I achieve this? But I want to show it only for the first row of the UITableView. Any suggestions?
if (indexPath.row == 0)
cell.backgroundColor = [UIColor greyColor];
Put it in :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if(indexPath.row == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.selected = YES;
}
...
}
Set the selectionStyle to Gray (this could be done in Interface Builder as well) and then set the cell as selected. Be sure to unselect the cell when it is reused.
You can do it in two ways:
First Way:
Why not put a view at the top as you want and place a tableview at the bottom of that view .
Second Way:
Customize a separate cell for your first row and use an if else condition when you are putting these cells in the table view .
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;
}
}
I am trying to color the background yellow when the string that is put into the cell equals a given word. I have been googling and asking friends and so far I have come up with this:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSRange flash = [[newsArray objectAtIndex:rad] rangeOfString: #"FLASH:"];
if(flash.length > 0) {
cell.backgroundColor = [UIColor yellowColor];
}
}
This does work but... it colors the wrong background. The one after the one I want to be colored. I have been told to put the coloring code inside cellForRowAtIndexPath instead but then it doesn't color anything.
Well that is it!
Hmmm - what's 'rad' in that code ([newsArray objectAtIndex:rad]) ?
Nowhere do I see you make use of 'indexPath' to determine if you should highlight that particular cell.
I need to do something like this :
.
Is it a UITableView? Or how should I do this? Any idea is welcome.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) return cell1;
} else if (indexPath.section == 1) {
if (indexPath.row == 0) return cell2;
} else if (indexPath.section == 2) {
if (indexPath.row == 0) return cell3;
}
}
Most likely its a UITableView. May be its not (it can be a UIScrollView with views added like sections in a table view).
But if you want to create something like in the image, I'd suggest you to use UITableView with customized cells. Because implementing table view, in this case, is simpler as you have to just concern about the delegate and the dateSource of the table view, rather than worrying about aligning the views in order.
Use sectioned table view with the translucent, gray-bordered, black-background-view as the backgroundView of the cells. Add the labels and arrow as the subviews of cell. You can add arrow as the accessoryView but that would become vertically centered, but in the image the arrow is displayed slightly on top of the cell.
There should be only one cell in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
There can be any number of sections (3 here).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3; // Can be any number
}
Yes, that's most likely a styled table view. See this guide or this nice tutorial.
why you worry about this one? Its simple man.... its nothing ,if you have hands over UITableView. Yeah, You must know about :
1: Set background image on view,exact as table size.
2: Set table view onto them and make background transparent.
3: Make customize table cell and add to table' row.
Thats it.....enjoy this work..its a creativity.
And you can do it guy....
I cannot tell exactly what is used from the picture, however I suggest using the UITableView especially if you have variable data coming from a structured object.
Some tutorials which I found very useful are the following:
Creating customized UITableViewCell from XIB
http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/
UIViewTable anti-patterns - A very good MVC pattern to build UIViewTable
http://www.slideshare.net/nullobject/uitableviewcontroller-antipatterns
And of course Apple's docs:
http:/developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
One important fact to remember is that you should always reuse cached table cells through the datasource method tableView:didSelectRowAtIndexPath:
Note: Since you want lots of transparencies there might be some performance issue especially on older devices.
Hope this helps and goodluck with your project.
There is not need to its scrollview or tableview... both are perform same..
Just change the Background Color of Tableview as clear color.....
check this code...
put this line to viewDidLoad :
[tableView setBackgroundColor:[UIColor clearColor]];
and
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
..... // Your label buttons etc........
......
......
.....
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setClipsToBounds:YES];
[[cell layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[cell layer] setCornerRadius:10];
}
You can do this using tableView in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//here place return your array contents
}
We can accomplish the similar as shown in image above by Table View and scroll view both.If you don't want to use table view then you can take a scroll view or a simple view and can add several buttons along with the image in background with different frames.It means button background will have the same image.then you can use different labels to show all the texts in different labels according to the requirement.
OR
you can use table view.For this you need to return number of sections 3(in this image) in the delegate method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.and then everything as the default tableview style.And then change the cell style to detailed.
yes this is a tableView with custom UITableViewCell.you can do that by creating a empty xib file and then add a UITableViewCell and do your customizing ...then in cellForRowAtIndexPath method use:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:edentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"yourCellName" owner:self options:nil]objectAtIndex:0];
this is from my application.
Yes, it is a groupedtableview with clearcolor as backgroundcolor. Cell background color also needs to be changed to clearcolor.
I suggest using div or list, don't use table, its very hard to control if the contents are too long.