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.
Related
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.
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.
So I'm loading a YouTube channel inside a tableView and trying to add the correct duration but sometimes it doesn't show up at all and when it does show up there's another text behind it if you look closely.
When you look at image 1 you can see that there's no time on one video until I scroll over it.
When you look at image 2 you can see that the text label is duplicated at some places
Code
- (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];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[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"];
}
}
return cell;
}
May be change it like-
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Then, the block -
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
if([totalTime length] <= 4) {
CGRect contentFrame6 = CGRectMake(0, 66, 30, 13);
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[imv5 release];
[title3 release];
} else if([totalTime length] == 5) {
CGRect contentFrame6 = CGRectMake(87, 64, 30, 13);
CGRect contentFrame7 = CGRectMake(90, 63, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.image=[UIImage imageNamed:#"blackBorder.png"];
imv5.alpha = 0.8;
[cell.contentView addSubview:imv5];
title3.tag = 0013;
title3.numberOfLines = 1;
title3.font = [UIFont systemFontOfSize:10];
[cell.contentView addSubview:title3];
title3.text = totalTime;
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
imv5.backgroundColor = [UIColor clearColor];
[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"];
}
}
And then, - return cell;
The settings i.e. the second block shall be done every time a Cell is dequed, not only when a new is allocated.
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
I'm trying to add title3 above the picture and into the black box on the left side.
Such as this
I'm not sure why it's not working but any help would be greatly appreciated. I'm pretty new to Objective-C so if you see anything that I'm doing wrong or that could be done better don't hesitate to tell me.
- (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
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];
});
}
CGRect contentFrame6 = CGRectMake(0, 67, 35, 13);
UIImageView *imv5 = [[UIImageView alloc]initWithFrame:contentFrame6];
imv5.alpha = 1;
imv5.tag = 0014;
[cell.contentView addSubview:imv5];
imv5.image = [UIImage imageNamed:#"blackBorder.png"];
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
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
NSString *word2 = #"00:00:";
if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:00:" withString:#"0:"];
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:"];
}
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.tag = 0013;
if([totalTime length] >= 5) {
CGRect contentFrame7 = CGRectMake(4, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.tag = 0013;
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.font = [UIFont boldSystemFontOfSize:11];
title3.text = totalTime;
[title3 release];
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
UILabel *title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.tag = 0013;
title3.backgroundColor = [UIColor clearColor];
title3.textColor = [UIColor whiteColor];
title3.font = [UIFont boldSystemFontOfSize:11];
title3.text = totalTime;
}
title3.numberOfLines = 1;
[cell.contentView addSubview:title3];
[title3 release];
} else {
//Update while scrolling
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];
// views and titles
UILabel *title = (UILabel *)[cell.contentView viewWithTag:0011];
UILabel *title2 = (UILabel *)[cell.contentView viewWithTag:0012];
title.text = [currentFeed title];
title2.text = [NSString stringWithFormat:#"%# views", convertedNumber];
totalTime = [self timeFormatted:([currentFeed duration].intValue)-1];
NSString *word = #":00:";
NSString *word2 = #"00:00:";
if ([totalTime rangeOfString:word].location == NSNotFound || [totalTime rangeOfString:word2].location != NSNotFound) {
totalTime = [totalTime stringByReplacingOccurrencesOfString:#"00:00:" withString:#"0:"];
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:"];
}
UILabel *title3 = (UILabel *)[cell.contentView viewWithTag:0013];
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.text = totalTime;
} else {
CGRect contentFrame7 = CGRectMake(6, 65, 60, 15);
title3 = [[UILabel alloc] initWithFrame:contentFrame7];
title3.textColor = [UIColor whiteColor];
title3.backgroundColor = [UIColor clearColor];
title3.text = totalTime;
}
NSLog(#"%# CHANGED TO : %#", [self timeFormatted:([currentFeed duration].intValue)-1], totalTime);
}
return cell;
}
I'm not certain it is the root of this particular problem, but you are attempting to update the UI from a background thread, and that is not allowed.
After you load your thumbnail in the background you need to marshal the UI update calls back to the main thread. Do this with another dispatch to the main queue (dispatch_get_main_queue()).