I have done many approach on changing font colors from a tableView cell. apparently both method failed. Unless I am doing it wrongly, then I need some help to correct it. Here is the code I found online to change the font color.
[[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];
I placed that line in the cellForRowAtIndexPath method and it did not work. Next, I searched this page and found another method to change font colors: tableView:willDisplayCell:forRowAtIndexPath: I copied and paste the setTextColor method into the willDisplayCell method and it did not work either. Below is the source code on which how I attempted and failed:
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"ApplicationCell";
ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"PharmacyCell" owner:self options:nil];
cell = tempCell;
self.tempCell = nil;
}
//cell.useDarkBackground = (indexPath.row % 2 == 0);
HealthWellness *objHealth = [self.maHealthArray objectAtIndex:indexPath.row];
cell.strPharmacyName = [objHealth.healthName stringByReplacingOccurrencesOfString:#"|" withString:#""];
[[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[[cell textLabel] setTextColor:[UIColor colorWithRed:255.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1]];
}
ApplicationCell.h
#interface ApplicationCell : UITableViewCell{
//variable declaration
BOOL useDarkBackground;
BOOL useMenuBackground;
NSString *strPharmacyName;
UIImage *pharmacyIcon;
NSString *strStoreName;}
//setting property
#property BOOL useDarkBackground;
#property BOOL useMenuBackground;
#property(retain) NSString *strPharmacyName;
#property(retain) UIImage *pharmacyIcon;
#property(retain) NSString *strStoreName;
ApplicationCell.m
- (void)setUseDarkBackground:(BOOL)flag{
if (flag != useDarkBackground || !self.backgroundView)
{
useDarkBackground = flag;
NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:useDarkBackground ? #"BGDark" : #"BGLight" ofType:#"png"];
UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
self.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundView.frame = self.bounds;
}}
- (void)setUseMenuBackground:(BOOL)flag{
if (flag != useDarkBackground || !self.backgroundView)
{
useDarkBackground = flag;
NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:useDarkBackground ? #"BGDark" : #"BGLight" ofType:#"png"];
UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
self.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.backgroundView.frame = self.bounds;
}}
You seem to be using a custom subclass of UITableViewCell and since you mention that there is a lot going on in the ApplicationCell class. I am guessing that your subclass isn't making use of the textLabel field since your changes aren't reflecting on the UI.
Check for the property outlet that is being displayed on screen from your ApplicationCell interface declaration & the NIB file. Once you've the appropriate property outlet, change its property.
That should set the text color, I just tried it and it works. cell should be a UITableView though, does your ApplicationCell inherit from UITableView cell?
Also if ApplicationCell is nil, I'm not sure whats going on with tempCell
try
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
and go from there
Related
Am creating a Rss feed app. Where i need to show Description (more than 5000 characters each). Am able to show all feeds in my app but my problem when i try to scroll the UITableView the scrolling not smooth i believe i need to make lazy loading for my content but am new to iphone development. Can any one show me correct way. And also set correct Cell Size according to the content size.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
if(cell.gestureRecognizers.count==0){
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longSelection:)];
longPress.minimumPressDuration=1.0;
[cell addGestureRecognizer:longPress];
}
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
cell.textLabel.textAlignment=NSTextAlignmentRight;
cell.textLabel.text=item.content;
cell.textLabel.numberOfLines=0;
cell.textLabel.font=[UIFont systemFontOfSize:15.0];
[cell.textLabel sizeToFit];
}
return cell;
}
Thanks
You have to use the -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
like so:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
//make sure to add the font you're using
CGSize stringSize = [item.content sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:14]
constrainedToSize:CGSizeMake(320, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
int rowHeight = stringSize.height;
return rowHeight;
}
Put this code:
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
Inside of here like so and remove it from where it is now (keeping it there causes you to have new imageviews created every time you scroll:
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
}
My tableview gets duplicated when I use [tableview reloadData].I used this to refresh the tableview when I move back.How could I overcome the issue?
This is my code.
#define kMyTag 1
#define despTag 2
#define subTag 3
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
UILabel *mainLabel=[[UILabel alloc]init];
mainLabel.tag = kMyTag; // define kMyTag in your header file using #define
mainLabel.frame=CGRectMake(95, 0, 170, 30);
mainLabel.font = [UIFont boldSystemFontOfSize:14];
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];
UILabel *despLabel=[[UILabel alloc]init];
despLabel.tag=despTag;
despLabel.frame=CGRectMake(95, 25, 190, 20);
despLabel.font= [UIFont systemFontOfSize:12];
despLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:despLabel];
} else {
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
switch([indexPath section]){
case 0:
{
NSString *urlString = [img objectAtIndex:indexPath.row];
urlString=[urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
tname=[mname objectAtIndex:indexPath.row];
tprice=[mprice objectAtIndex:indexPath.row];
tquan=[mquan objectAtIndex:indexPath.row];
tspice=[mspice objectAtIndex:indexPath.row];
UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
mainLabel.text = [NSString stringWithFormat:#"%#",tname];
UILabel *despLabel=(UILabel *)[cell.contentView viewWithTag: despTag];
despLabel.text = [NSString stringWithFormat:#"Qty :%# Spice: %#",tquan,tspice];
UILabel *subLabel=(UILabel *)[cell.contentView viewWithTag: subTag];
float pric=[tprice floatValue];
subLabel.text = [NSString stringWithFormat:#"Price :%# %.2f",currencyType, pric];
}
}
}
Overcomes the issue by removing all the objects from array,then reselects the DB table & reloads uitableview.
it happens because you are not passing any value in dequeueReusableCellWithIdentifier
pass a string value and then use it in reuseIdentifier also
I created a custom cell which contains a label and textfield.I am using this custom cell in table view. When i enter the data in text field and when i scroll the view my data is getting replaced.For every scroll it getting replaced/erased.Any one tell me where i am doing mistake.
My custom cell code goes here.Thanks!
-(UITableViewCell *)returnCellForEach:(UITableView *)tableView
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCellToAddDetails" owner:self options:nil];
cell = customCell;
// customCell = nil;
customLabel = (UILabel *)[customCell.contentView viewWithTag:100];
}
return cell;
}
-(UITextField *)dataField
{
UITextField *textField = customField;
return textField;
}
You will need to store the data input by user somewhere related to particular indexpath and When you scroll through the cells you will first need to check if there is any dataObject corresponding to that indexpath and accordingly set the data. You can use a dictionary to store your data corresponding to particular cell. Hope it helps
I am not familiar with the method names that you are using, in my case I needed to create a custom Table view Cell Class outside the tableview class,
the class is called QRTYPECELL and following are the relevant codes:
#implementation QRTypeCell
#synthesize imageView;
#synthesize labelview;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
imageView = nil;
labelview= nil;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)];
imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:imageView];
self.contentView.backgroundColor = [UIColor blackColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.textColor = [UIColor greenColor];
label.backgroundColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:17];
self.labelview = label;
[self.contentView addSubview:label];
}
return self;}
And then in the TableView Class:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
QRTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[QRTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
CurrentQRCode = (QRCode *)[fetchedResultsController objectAtIndexPath:indexPath];
NSString *icon = CurrentQRCode.parsed_icon;
NSString *string = CurrentQRCode.parsed_string;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:#"png"];
UIImage *image_file = [UIImage imageWithContentsOfFile:filePath];
in IB you see a Identifier field place any value in that and use that value as a reuse Identifier.
i think this will helps you.
ok i got it did you create class for customCell and replace this line
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
with
CustomCell *customCell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I have a custom tableview to load news through webservice. I change image in each cell in method cellForRowAtIndexPath. After I got the right cell, I can access the image by two ways:
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
or directly:
cell.imageView.image = news.image;
when I use the second way, some images get into wrong cell while the first way give me a perfect result. It also happend in a callback method iconDownLoadFinsh.
I feel confused about it. Can anyone try to explain it? Please do not hesitate to ask questions. Thanks in advance.
- (void)iconDownLoadFinsh:(NSData *)imageData row:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = [UIImage imageWithData:imageData];
NewsModel *newsModel = [newsArray objectAtIndex:indexPath.row];
newsModel.image = [UIImage imageWithData:imageData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
// read from newsModel
NewsModel *news = [newsArray objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:10];
label.text = [NSString stringWithFormat:news.title];
label = (UILabel *)[cell viewWithTag:11];
label.text = [NSString stringWithFormat:news.description];
UIImageView *imageView;
imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
if (news.image == nil)
{
imageView.image = [UIImage imageNamed:IconPlaceHolder];
// if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
IconDownLoader *iconDownLoader = [[IconDownLoader alloc] init];
iconDownLoader.url = news.imageUrl;
iconDownLoader.delegate = self;
iconDownLoader.indexPath = indexPath;
[downloadArray addObject:iconDownLoader];
[iconDownLoader start];
// }
}
return cell;
}
I generally always reset the cell properties in the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
So I will do the following after getting the cell
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
cell.imageView.image = nil; // Here resetting the imageview's image to nil.
Because you are reusing cells and if you do not clear old data, the cell data may get corrupt.
I'm sure this is a question that been asked and answered, but I can't seem to find what I need. I am trying to customize the background of my UITableView, so to do this, I created a subclass of UITableViewCell, and built my custom cell with the IB. I successfully connected the custom cell to the table view, so the cell I get is getting displayed. I am trying to change the background of each cell, so here is what I use:
if(row == 1){
rowBackground = [UIImage imageNamed:#"VehicleExpenses_button.png"];
selectionBackground = [UIImage imageNamed:#"VehicleExpenses_button_selected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
// ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
within the
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method of my Controller class. I checked with the debugger, and all the lines in the code are getting called...
Any ideas?
This is the entire method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HomeViewCell";
static NSString *CellNib = #"HomeViewCell";
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger row = [indexPath row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
if(row == 1){
rowBackground = [UIImage imageNamed:#"VehicleExpenses_button.png"];
selectionBackground = [UIImage imageNamed:#"VehicleExpenses_button_selected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
// ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
// perform additional custom work...
return cell;
}
Maybe I'm wrong but I don't see where you initialized the backgroundView as a UIImageView. Here is what I would add to the lines where you create the cells:
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
}
Note: replace "cellHeight" with your desired height.
I hope this helps.
You can use this:
UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"selectedRow.png"]];
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround];
[cellBackGround release];
Small problem. Instead of casting and setting the backgroundView and selectedBackgroundView's image property, set them to the UIImageView created from your images.
cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease];