image in cell overlaying text - objective-c

The image seems to overlay the text in the cell, I'm not sure why this is happening but I'd like the text to go over the image. Any help is greatly appreciated.
This is how my code looks like.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
CGRect contentFrame = CGRectMake(100, 2, 198, 30);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:title];
[title release];
}
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
title.text = [currentFeed title];
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
cell.imageView.image = myview;
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
}
return cell;
}

cell.imageView.image is not your self.customImage you added.Now it displayes the tableview cel default imageview in cell
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage setTag:12345];
[self.customImage release];
and get the added custom imageview as
UIImageView *imageVW=[cell viewWithTag:12345];
and use this imageview to set image later in the code instead of cell.imageview

Related

UIImageView transparent black on a cell bug (Objective-C)

I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.

Labels duplicating?

I'm trying to add a timer on the side of the thumbnail but the UIlabel for the timer is always duplicating when I scroll.
It ends up like this. Any help would be greatly appreciated.
This is how my method looks like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
title.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:title];
[title release];
// Views Placement
CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
title2.tag = 0012;
title2.numberOfLines = 1;
title2.backgroundColor = [UIColor clearColor];
title2.textColor = [UIColor whiteColor];
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
//Here's where the fails come in
CGRect contentFrame6 = CGRectMake(0, 66, 33, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5.alpha = 1;
title3.tag = 0013;
imv5.tag = 0014;
[cell.contentView addSubview:imv5];
imv5.backgroundColor = [UIColor clearColor];
}
// Formatting views
NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber * formattedViews = [f numberFromString
:[currentFeed views]]; [f release]; NSNumber *firstNumber = formattedViews; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];[formatter setNumberStyle: NSNumberFormatterDecimalStyle]; convertedNumber = [formatter stringForObjectValue:firstNumber];
// Basic stuff
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
UILabel *title3 = (UILabel *)[cell.contentView viewWithTag:0013];
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
if ([totalTime rangeOfString:word].location == NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:" withString:#""];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"01:" withString:#"1:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"02:" withString:#"2:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"03:" withString:#"3:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"04:" withString:#"4:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"05:" withString:#"5:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"06:" withString:#"6:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"07:" withString:#"7:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"08:" withString:#"8:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"09:" withString:#"9:"];}
if([totalTime length] >= 5) {
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.font = [UIFont boldSystemFontOfSize:10];
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.font = [UIFont boldSystemFontOfSize:10];
}
title3.text = totalTime;
[cell.contentView addSubview:title3];
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
cell.imageView.image = myview;
//Add Black fucking border and stuff here!!!
}else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:[currentFeed thumbnailURL]];
UIImage *thumbnail = [UIImage imageWithData:data];
cell.imageView.image = thumbnail;
[self.feedsTableView beginUpdates];
[self.feedsTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
withRowAnimation:UITableViewRowAnimationNone];
[self.feedsTableView endUpdates];
});
}
return cell;
}
Obvious:
Don't add any views outside the (cell == nil) block.
Cells are reused, that means that if you add any label to a reused cell, there will be two labels... and more if the cell is reused again.
There are many other problematic parts. For example, starting cell reloading from inside this method is VERY strange.
Also, the code would be simplified a lot, if you used a xib for the cell design.

tableView cells refreshing auto?

When I scroll in my tableview the cell seems to be refreshing because the duration in the left corner sometimes shows up and sometimes it doesn't. Does anyone know how I can make it stay at one place ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Getting XML data
static NSString *CellIdentifier = #"Cell";
ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Adding thumbnail
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title Placement
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:title];
[title release];
// Views Placement
CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
title2.tag = 0012;
title2.numberOfLines = 1;
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
// Dislike Bar
CGRect contentFrame4 = CGRectMake(123, 54, 190, 5);
UIImageView *imv2 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv2.image=[UIImage imageNamed:#"redbar.png"];
[cell.contentView addSubview:imv2];
[imv2 release];
// Like Bar
CGRect contentFrame3 = CGRectMake(123, 54, 38 * [currentFeed rating].floatValue, 5);
UIImageView *imv = [[UIImageView alloc]initWithFrame:contentFrame3];
imv.image=[UIImage imageNamed:#"greenbar.png"];
[cell.contentView addSubview:imv];
[imv release];
// No Likes / Disabled rating
if([currentFeed rating].intValue == 0) {
UIImageView *imv3 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv3.image=[UIImage imageNamed:#"graybar.png"];
[cell.contentView addSubview:imv3];
[imv3 release];}
// Formatting duration
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
if ([totalTime rangeOfString:word].location == NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:" withString:#""];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"01:" withString:#"1:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"02:" withString:#"2:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"03:" withString:#"3:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"04:" withString:#"4:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"05:" withString:#"5:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"06:" withString:#"6:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"07:" withString:#"7:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"08:" withString:#"8:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"09:" withString:#"9:"];}
// Adding time/Black border
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
//[cell.contentView addSubview:title3];
//[cell.contentView addSubview:imv5];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
}
}
// Adding title/views
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
// Formatting views
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * formattedViews = [f numberFromString:[currentFeed views]];
[f release];
NSNumber *firstNumber = formattedViews;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
// Reoloring / Adding value to text
title2.text = [NSString stringWithFormat:#"%# views", convertNumber];
title2.backgroundColor = [UIColor clearColor];
title2.textColor= [UIColor whiteColor];
title.text = [currentFeed title];
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
// Caching images and adding them as thumbnails
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
// Adding thumbnail
cell.imageView.image = myview;
//Adding duration time/blakhover
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
//Adding duration time/blakhover over the image
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}
return cell;
}
EDIT:
After following you guys this happened
I tried using your solution and it ended up like this with this code, but when I scroll down everything goes back to normal. Any ideas ?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Getting XML data
static NSString *CellIdentifier = #"Cell";
ChanelFeeds *currentFeed = [[xmlParser feeds] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Adding thumbnail
CGRect imageFrame = CGRectMake(0, 0, 120, 90);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
[cell.contentView addSubview:self.customImage];
[self.customImage release];
// Title Placement
CGRect contentFrame = CGRectMake(122, 2, 198, 45);
UILabel *title = [[UILabel alloc] initWithFrame:contentFrame];
title.tag = 0011;
title.numberOfLines = 2;
title.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:title];
[title release];
// Views Placement
CGRect contentFrame2 = CGRectMake(127, 70, 180, 15);
UILabel *title2 = [[UILabel alloc] initWithFrame:contentFrame2];
title2.tag = 0012;
title2.numberOfLines = 1;
title2.font = [UIFont italicSystemFontOfSize:14];
[cell.contentView addSubview:title2];
[title2 release];
// Dislike Bar
CGRect contentFrame4 = CGRectMake(123, 54, 190, 5);
UIImageView *imv2 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv2.image=[UIImage imageNamed:#"redbar.png"];
[cell.contentView addSubview:imv2];
[imv2 release];
// Like Bar
CGRect contentFrame3 = CGRectMake(123, 54, 38 * [currentFeed rating].floatValue, 5);
UIImageView *imv = [[UIImageView alloc]initWithFrame:contentFrame3];
imv.image=[UIImage imageNamed:#"greenbar.png"];
[cell.contentView addSubview:imv];
[imv release];
// No Likes / Disabled rating
if([currentFeed rating].intValue == 0) {
UIImageView *imv3 = [[UIImageView alloc]initWithFrame:contentFrame4];
imv3.image=[UIImage imageNamed:#"graybar.png"];
[cell.contentView addSubview:imv3];
[imv3 release];}
// Formatting views
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * formattedViews = [f numberFromString:[currentFeed views]];
[f release];
NSNumber *firstNumber = formattedViews;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *convertNumber = [formatter stringForObjectValue:firstNumber];
// Reoloring / Adding value to text
title2.text = [NSString stringWithFormat:#"%# views", convertNumber];
title2.backgroundColor = [UIColor clearColor];
title2.textColor= [UIColor whiteColor];
title.text = [currentFeed title];
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
}
else
{
// Adding title/views
// Caching images and adding them as thumbnails
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *imgURL = [currentFeed thumbnailURL];
NSArray *parts = [[NSString stringWithFormat:#"%#", imgURL] componentsSeparatedByString:#"/"];
NSString *imageName = [parts objectAtIndex:[parts count]-2];
NSString *filePath = [directoryPath stringByAppendingPathComponent:imageName];
UIImage *myview = [UIImage imageWithContentsOfFile:filePath];
if(myview){
// Adding thumbnail
cell.imageView.image = myview;
//Adding duration time/blakhover
// Formatting duration
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
if ([totalTime rangeOfString:word].location == NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:" withString:#""];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"01:" withString:#"1:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"02:" withString:#"2:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"03:" withString:#"3:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"04:" withString:#"4:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"05:" withString:#"5:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"06:" withString:#"6:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"07:" withString:#"7:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"08:" withString:#"8:"];
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"09:" withString:#"9:"];}
// Adding time/Black border
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
//[cell.contentView addSubview:title3];
//[cell.contentView addSubview:imv5];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
[imv5 release];
[title3 release];
}
}else{
NSData* imageDataTemp = [[NSData alloc] initWithContentsOfURL:[currentFeed thumbnailURL]];
if(imageDataTemp){
cell.imageView.image = [UIImage imageWithData:imageDataTemp];
}else{
cell.imageView.image = [UIImage imageNamed:#"youtubeLogo.png"];
}
//Adding duration time/blakhover over the image
[cell.contentView addSubview:imv5];
[cell.contentView addSubview:title3];
}
}
return cell;
}
You set your time label's text inside of if (cell == nil) block, which means that it stays the same when cell is reused.

Setting backgroundView of UITableView cell

I'm setting the background of a UITableViewCell and for whatever reason it will not appear.
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:15];
cell.textLabel.text = [[_comments objectAtIndex:indexPath.row] text];
((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:#"bubble.png"];
cell.imageView.image = [UIImage imageNamed:#"user.png"];
return cell;
Everything but the cell.backgroundView works. Am I doing something wrong?
Shouldn't have type casted it.
Solution was to set a UIImageView image and then set it directly.
UIImageView *bgView = [[UIImageView alloc] init];
(bgView).image = [UIImage imageNamed:#"bubble.png"];
cell.backgroundView = bgView;
[bgView release];

UITableviewcell loading data issue

I parsed the XML data from a feed. I stored all data in NSArray. I loaded the images and title in UITableview using UILable. But when I use the scroll, the text will be collapsed as follows.
My code is as follows...
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.text=[title1 objectAtIndex:indexPath.row];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview:valueField];
[valueField release];
UIImage *patternImage = [UIImage imageNamed:#"bg.jpg"];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
cell.backgroundView = backView;
NSString *image1 =[images objectAtIndex:indexPath.row];
NSLog(#"Images ..... = %#",image1);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
imageView.frame = CGRectMake(0.0f, 00.0f, 60.0f, 63.0f);
[cell.contentView addSubview:imageView];
//cell.imageView.image = myimage;
return cell;
}
I don't know how to solve this issue.
Every time the cell is reloaded, the UILabel is added.
Do something like
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview:valueField];
[valueField release];
}
valueField.text=[title1 objectAtIndex:indexPath.row];
To be more specific, since you are dequeuing cells, when you call cellForRowAtIndexPath it potentially can return you a cell that was already created.
That cell already has a label created.
So all of the magic of creating and setting up the custom views in you cell should only be done when dequeueReusableCellWithIdentifier returns a nil value