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.
Related
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];
I receive a color from the server and in cellForRowAtIndexPath method I'm trying to apply this color to a view inside the cell.
The problem is that all the cells display the same color until I scroll down the table. When I start scrolling they update well their color.
I'm new on iOS and Objective-C, so if you could help me it would be appreciated, thanks.
Before scrolling:
After scrolling:
Some code: (If you want more please tell me)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CalendarDayCell *cell = (CalendarDayCell *) [tableView dequeueReusableCellWithIdentifier:#"CalendarDayCell"];
if (cell == nil) {
cell = [[CalendarDayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CalendarDayCell"];
}
if (self.events.count) {
CalendarEvent *ce = self.events[indexPath.row];
CalendarDayCell *dayCell = (CalendarDayCell *) cell;
// ...
dayCell.viewColorBar.backgroundColor = [self colorWithHexString:ce.color];
return dayCell;
} else {
// Not important
}
}
PS: I've also tried it at willDisplayCell method with the same results.
EDIT:
Finally I figured out what were the solutions.
IDK why XCode redimensioned the color bar height to be 980px from the 50px that I specified in the nib file. And that was causing that all cells below had the same color.
I put all the views in another view, and assigned that view to the cell because some cells were not showing their color.
that's all
The problem is with reusing the cells - that means that in cellForRowAtIndex path you have to set color for every condition. This is easily reproducible with images. If you set image only sometimes, you will have to set image to none when you don't need to display it. What you need to do is to handle in the else block the control you want to change.
if(self.events.count){
dayCell.viewColorBar.backgroundColor = [self colorWithHexString:ce.color];
}
else{
dayCell.viewColorBar.backgroundColor = [UIColor clearColor];
}
Or something like that.
Are you sure that self.events.count is actually non-zero? Perhaps the tableview is being populated before self.events is setup properly.
For example, the view controller might be loading via viewDidLoad, then the UITableView, then you're setting self.events. If that's the case, self.events will be nil until the whole view/viewcontroller is loaded. That would account for it working after you start scrolling.
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 need to make a grouped table view with custom cells. Each cell must have a background image, so that image of one cell will touch the image of the second cell and so on. I've tried to set the separatorStyle to None, but I still get the transparent separator between cells.
Help me, please, remove this space between cells.
Have a good day,
Thanks!
I found that when I set the background image, the separator went away automatically. The problem is that I want it to show up.
Here's what I did:
Create a custom UITableViewCell class. In the respective init method do:
// Create a background image view.
self.backgroundView = [[UIImageView alloc] init];
Then in your controller that manages the UITableView:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// After all of the cell dequeue/allocation...
UIImageView *backgroundView = (UIImageView *)cell.backgroundView;
backgroundView.image = [UIImage imageNamed:#"your_image"];
}
So, the correct number of rows shows up. On pressing any row, the correct action takes place. However, the cells themselves are nowhere to be seen.
I added a NSTimer in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for each cell just to trace it out beyond the function - they all say that their superview is equal to the tableview in question (and is not nil, so i'm not checking nil == nil or something).
They all contain labels with the correct text.
The separator lines are being drawn.... If I change the TableView background, the whole visible area's background shows as that color.
I'm checking that each cell is neither hidden nor set to an alpha of 0.
Is there anything else I could be missing?
Are you loading from your cells from a nib file or creating programmatically?
Are you overlaying another object over your cell in the cell subview? Perhaps a subview is covering it; I can't tell, since you have not posted any code yet. Given the information you have provided, it is difficult to determine why you cannot see the cells backgroundView.
Try changing the color with
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
tmpView.backgroundColor = [UIColor blackColor];
myCell.backgroundView = tmpView;
It sounds like you have set the backgroundView of your cell to [UIColor clearColor].