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];
}
Related
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){....
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];
}
I'm trying to add a UITextField as a subview to my table cells. The content of the text fields is fine until I start scrolling and the cells start to be reused. The images illustrate the problem.
At first, the blue values on the right in the UITextField are correct, i.e. the value corresponds to the row number. The second and third images, scrolled down and back up, show that the values are being reused in odd ways.
How do I avoid this? Using unique values for reuseIdentifier solves this problem, but obviously it's not very efficient.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITextField *numberTextField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(200, 10, 95, 30)];
numberTextField.adjustsFontSizeToFitWidth = YES;
numberTextField.textColor = [UIColor blueColor];
numberTextField.placeholder = #"Enter value";
numberTextField.keyboardType = UIKeyboardTypeDecimalPad;
numberTextField.tag = ([indexPath row]+1);
numberTextField.backgroundColor = [cell backgroundColor];
numberTextField.textAlignment = NSTextAlignmentRight;
numberTextField.clearButtonMode = UITextFieldViewModeNever;
numberTextField.clearsOnBeginEditing = YES;
[numberTextField setEnabled:YES];
[cell addSubview:numberTextField];
} else {
numberTextField = (UITextField *)[cell.contentView viewWithTag:([indexPath row]+1)];
}
cell.textLabel.text = [NSString stringWithFormat:#"Row %i",[indexPath row]+1];
numberTextField.text = [NSString stringWithFormat:#"Value: %i",[indexPath row]+1];
return cell;
}
The problem is you only assign the tag to the numberTextField when it is created. If it gets reused, it doesn't get its tag reassigned.
You should use a constant tag number for the UITextField instead of using row+1.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 33)];
dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(105, 0, 320, 33)];
timeLabel.backgroundColor = [UIColor clearColor];
dataLabel.backgroundColor = [UIColor clearColor];
dataLabel.textColor = [UIColor whiteColor];
timeLabel.textColor = [UIColor whiteColor];
[cell.contentView addSubview:timeLabel];
[cell.contentView addSubview:dataLabel];
if(indexPath.row == 0)
{
if([storedDataArray count]>0)
{
timeLabel.text = currenttime;
dataLabel.text = textView1.text;
}
return cell; //returns empty cell if there is no data in the data base
}
if(indexPath.row == 1)
{
timeLabel.text = currenttime;
dataLabel.text = textView2.text;
return cell;
}
I have a table view
- i want to display my textlabel on left side, just beside detailTextlabel.
actually when i use textLabel.text, DetailTextLabel.Text and alignments,
they displayed but not at the position i want.
so i tried to use own labels..
but they get overwritten.
since we did not (it's not possible to) change the possition & the frame size of the cell textlabel, detailTextLabel, i added 2 labels with a frame size that i want.
but when we entered text in text views and return back to table view.. the labels get overwritten..
what to do..
is there any alternate to fixed the frame size of my text label
or to avoid overwriting of our labels
Use Custom UITableViewCell instead. You can align and place labels easily using custom tabe cells.
This link will be helpful. http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1
look at the customizing section in the document.
use this code it will work perfectly
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
{
NSArray *arrayView = [cell.contentView subviews];
for (UIView *vTemp in arrayView)
{
[vTemp removeFromSuperview];
}
NSLog(#"---No New Cell hiiii");
// Setting Tag To UIButton
_checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
_cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
add else part only
Hey all, I'm trying to create a business finder style app and was hoping to create a custom cell to display some information to the user. I've got the cell to display all the information I'm trying to give, but the format is off. Here is the code I'm using to create the cell.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellView" owner:self options:nil];
#ifdef __IPHONE_2_1
cell = (CustomCell *)[nib objectAtIndex:0];
#else
cell = (CustomCell *)[nib objectAtIndex:1];
#endif
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
cell.bizNameLabel.text = [dict objectForKey:#"name"];
cell.addressLabel.text = [dict objectForKey:#"address"];
NSMutableString *detailed = [NSMutableString string];
[detailed appendString:[dict objectForKey:#"distance"]];
[detailed appendString:#"mi"];
cell.mileageLabel.text = detailed;
// determine the rating for the business
NSString *icontype = [dict objectForKey:#"rating"];
NSInteger rating = [icontype intValue];
UIImage *image;
// variable to change the color of the cell's background
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
// switch statement to determine the rating image to be displayed
switch (rating) {
case 1:
{
image = [UIImage imageNamed:#"1.png"];
bg.backgroundColor = [UIColor yellowColor];
break;
}
case 3:
{
image = [UIImage imageNamed:#"3.png"];
bg.backgroundColor = [UIColor orangeColor];
break;
}
case 5:
{
image = [UIImage imageNamed:#"5.png"];
//bg.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
break;
}
default:
break;
}
[cell.ratingImage setImage:image];
cell.backgroundView = bg;
[bg release];
#ifdef __IPHONE_3_0
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
#endif
return cell;
}
I then created the layout in IB to look like this..
(First 2 issues have been resolved, I was using the wrong image variable to display the image)
And when selected, you can tell things are out of whack
![enter image description here][3]
In IB I have the dimensions of the cell set at 320wide by 80high, and under the indentity tab, I've changed the class to CustomClass. I'm sure I'm overlooking something trivial, but if someone could throw me a bone, I'd be grateful. I've fixed the problem I was having with the image not displaying where I wanted it to, but I'm still having issues with the background color not changing, font size displaying different and when the cell is selected, it overlaps the cell separator.
Note: tried to include screen shots, but since I'm new SO wouldn't let me. I've provided a link where I've put the image of the selected cell that overlaps the separator below.
http://s1216.photobucket.com/albums/dd361/cabearsfan/?action=view¤t=screenshot2.png
Seems like you are not using your nib reference of the UIImageView but you are using the default imageView property of the cell.
I'd say there is a mis-match between the imageview frame and the size of the image you're giving it.
After your setImage call, add this code to find out what's going on:
NSLog(#" imageView frame %f,%f, %f, %f",imageView.frame.origin.x,imageView.frame.origin.y,
imageView.frame.size.width,imageView.frame.size.height);
NSLog(#" image size %f x %f", image.size.width, image.size.height);