Reputation: 175
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;
}
Upvotes: 0
Views: 73
Reputation: 130102
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.
Upvotes: 2