UICollectionviewcell change background - iphone

How to change the background in the cell if I know the section number and the item number? The code below shows how I tried to do it.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:#"%i",indexPath.item-dayStart+2];
if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){
cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
cell.titleLabel.textColor = [UIColor whiteColor];
}
return cell;
But if I do so during scrolling painted not just the relevant cell.

What you're seeing is the cell getting reused as the view scrolls, the reused cell still has the background color from an earlier use. Fix by handling both branches of the case (all cases) when you configure, e.g.:
if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){
cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
cell.titleLabel.textColor = [UIColor whiteColor];
} else {
cell.backgroundColor = [UIColor whiteColor]; // whatever the default color is
}
If you're using a custom subclass of UICollectionViewCell, you may also reset to defaults by implementing the prepareForReuse method.

you are using CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
the collection view is dequeueing ReusableCells so when scrolling it than again uses the old cells changing your desired requirement.it happens with background color and images
add this line before condition
CalendarCollectionViewCell *cell=[NSBundle mainBundle] loadNibNamed:#"CalendarCollectionViewCell" owner:self options:nil] objectAtIndex:0];
do not use
if(!cell){....

Related

UICollectionViewCell change background color

I have UICollectionview with sections and two cell class.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCell *cell;
firstDayInMonth = [self dayWeekStart:[self getDateFromItem:dateFromStart section:indexPath.section row:1]];
if (indexPath.row < firstDayInMonth) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellCalendarEmpty" forIndexPath:indexPath];
} else {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellCalendar" forIndexPath:indexPath];
}
}
I have start item, section and period. Beginning from the start section and item i need to change background color cell depending on period. i need to change only cellCalendar. I use cellCalendarEmpty to move first cellCalendar.
Fix by handling both branches of the case (all cases) when you configure, e.g.:
if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){
cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
cell.titleLabel.textColor = [UIColor whiteColor];
} else {
cell.backgroundColor = [UIColor whiteColor]; // whatever the default color is
}
If you're using a custom subclass of UICollectionViewCell, you may also reset to defaults by implementing the prepareForReuse method.

UIScrollView issues with UITableview

I have one UITableview and UICutsom cell. In that UITableview I populate data using web services. I change background color and font color of cell. When I scroll up and down of that Tableview cell background color and label color also change on another cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"FieldInventoryCell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
FieldInventoryCell *cell=(FieldInventoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:#"FieldInventoryCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
NSArray *ar = [[arr objectAtIndex:indexPath.row]componentsSeparatedByString:#"|"];
cell.Status.text=[ar objectAtIndex:1];
cell.serialnumber.text=[ar objectAtIndex:0];
cell.date.text=[ar objectAtIndex:2];
if([cell.serialnumber.text isEqualToString:#"Grand Total"])
{
cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor whiteColor];
}
if ([cell.serialnumber.text isEqualToString:#"INV_FLD Status Total"])
{
//cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor orangeColor];
}
return cell;
}
As far as I can understand from your post, the problem you're probably experiencing is connected with dequeuing cells. You're setting some colors for the contentView and the serial number label, but you're not resetting them if the conditions are not met. Since the table reuses cells, your already colored items will appear for new elements. To ammend the problem, reset the colors to default, i.e. something like
// ...
if([cell.serialnumber.text isEqualToString:#"Grand Total"])
{
cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor whiteColor];
} else {
// reset color
cell.contentView.backgroundColor=[UIColor clearColor];
cell.serialnumber.textColor=[UIColor blackColor];
}
if ([cell.serialnumber.text isEqualToString:#"INV_FLD Status Total"])
{
//cell.contentView.backgroundColor=[UIColor blueColor];
cell.serialnumber.textColor=[UIColor orangeColor];
} else {
// reset color
cell.serialnumber.textColor=[UIColor blackColor];
}

Cell reuse issue in table view iphone

I know this is a repeated question,but im not getting the solution for this problem.
Im using a tableview which i want to change the color of one textlabel according to some status form webservice.
But the problem is every text label color is changing when the table view is scrolled up and down.This is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [[self.Contents objectAtIndex:indexPath.row]event];
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"]){
cell.textLabel.textColor = [UIColor redColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
When the cell ==nil condition is commented it is working fine.But i know it is not the exact way to do this.
Can anyone please help me with this.
Thanks in advance.
If you're reusing a cell, that cell (and all of its properties) are the same as they were before.
So, your code
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"])
{
cell.textLabel.textColor = [UIColor redColor];
}
will set the text red in that condition. If the condition is false, then the text colour is not changed. However, if the colour was changed to red before, it is still red. Just add code to set it to the appropriate colour in the other condition.
if([[[self.Contents objectAtIndex:indexPath.row]status] isEqualToString:#"Failed"])
{
cell.textLabel.textColor = [UIColor redColor];
}
else
{
cell.textLabel.textColor = [UIColor blackColor];
}

Setting background image of custom table view cells

I've tried numerous ways of setting the background image of an unselected table cell, but without success:
1- In IB setting the image field
2- cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"list.png"]];
3- cell.imageView.image = [UIImage imageNamed:#"list_selected.png"];
All seem to fail. The image setting for selected cell works, but not for an unselected cell. Anyone having any idea what might be wrong here?
Thanks
Try setting the backgroundView to an imageView of the image.
Code example from Jason Moore (with TomH's correction):
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]] autorelease];
cell.backgroundImage no longer exists. Instead use:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]];
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
I've been doing this using the following UITableViewDelegate method, and setting the cell.backgroundColor property. It gets called last, right before the cell is actually drawn on the screen:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2)
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"TableCell-BG-Dark.png"]];
else
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"TableCell-BG-Light.png"]];
}
And yes, I'm using a custom subclass of UITableViewCell.
try doing this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DateCellIdentifier] autorelease]
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"gradient.png"]];
[cell setBackgroundView:img];
[img release];
}
cell.textLabel.text = #"Text";
}
I've also had problems with trying to change the background colour of cells, i ended up subclassing the cell for different reasons, this is my cell background alternation code:
if (indexPath.row % 2 == 0) {
cell.contentView.backgroundColor = [UIColor colorWithWhite:230.0/255 alpha:1];
} else {
cell.contentView.backgroundColor = [UIColor colorWithWhite:242.0/255 alpha:1];
}

Setting background color of a table view cell on iPhone

I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and so on... could you let me know how can I do that?
Thanks.
Add this method to your table view delegate:
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor = indexPath.row % 2
? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0]
: [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
You have to set the background color of the cell's content view
cell.contentView.backgroundColor = [UIColor colorWithRed...]
This will set the background of the whole cell.
To do this for alternate cells, use the indexPath.row and % by 2.
If you want to set cell color based on some state in the actual cell data object, then this is another approach:
If you add this method to your table view delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
Then in your cellForRowAtIndexPath method you can do:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
This saves having to retrieve your data objects again in the willDisplayCell method.
please add the following code in the cellForRowAtIndexPath
if (indexPath.row % 2 == 0){
cell.backgroundColor =[UIColor blueColor];
} else {
cell.backgroundColor =[UIColor whiteColor];
}
i think this will help you
The cell.contentView.backgroundColor = [UIColor colorWithRed...] method in lostInTransit's answer works, as long as you do not use the built-in label of a UITableViewCell.
I found that if you use the built-in label, e.g. by setting cell.text, you end up with a opaque white block under the label and only the two ends of the cell show your desired color.
I found no way to edit the built-in label so it is non-opaque (you can access it via UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]).
I solved the problem by adding my own custom UILabel. Like this:
UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
cellLabel.text = #"Test with non-opaque label";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;
[cell.contentView addSubview:cellLabel];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
When using the default table cell setting the following two properties will color both the cell's background and the background color of it's label, avoiding the need to create a custom label as a subview of the cell's contentView.
cell.textLabel.backgroundColor = [UIColor redColor];
cell.contentView.backgroundColor = [UIColor redColor];
//time saving method:
//in cell for index method:
static NSString* evenIdentifier = #"even";
static NSString* oddIdentifier = #"odd";
__weak identifier;
if(indexPath.row %2 ==0)
{
identifier = evenIdentifier;
}else{
identifier = oddIdentifier;
}
cell = dequeue..WithIdentifier: identifier;
if(cell == nil){
cell = allocOrLoadNib...;
cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor);
}
// change the cell content and then return it. Easy job.
// this is a outline code. Please not copy it directly.
This worked for me .. In cellForRowAtIndexPath ,
cell.textLabel.backgroundColor = [UIColor clear];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want
cell.backgroundColor = cell.contentView.backgroundColor;
For you requirement, as mentioned earlier based on indexPath % 2 value you can perform the above function.
This code is a slightly more clean solution for the case where you are using the built-in text label and don't want the white background color of the text label obscuring the background color. This also fixes the issue of violating the rounded corners of a grouped style of table view (which happens when you use cell.contentView.backgroundColor instead of cell.backgroundColor) :
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1];
cell.backgroundView = bg;
[bg release];
All you need to do is add the following code in your table's view controller implementation file.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor blackColor];
cell.backgroundColor = altCellColor;
}}
It's then as simple as adding and changing the modulus values.
Add backgroundView to the cell and set its background color of your choice.
let backgroundView = View() // add background view via code or via storyboard
view.backgroundColor = UIColor.red // your color
cell.backgroundView = backgroundView