-1

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;
}

Duration only showing on some places

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;
}

This is how it loads up

IamGretar
  • 173
  • 2
  • 13
  • It reuse Cell, http://stackoverflow.com/questions/2152180/iphone-what-are-reuseidentifiers-uitableviewcell..:) –  Jun 14 '13 at 14:54
  • That's how table views work. It reuses the same cells for the sake of memory management. You don't want the cells to remain static. – The Kraken Jun 14 '13 at 14:56
  • If someone can rearrange the code for me and make it work I'll pay him $10 via PayPal!!! – IamGretar Jun 14 '13 at 19:32

1 Answers1

0

You set your time label's text inside of if (cell == nil) block, which means that it stays the same when cell is reused.

Kreiri
  • 7,840
  • 5
  • 30
  • 36