0

我正在尝试在缩略图的一侧添加一个计时器,但是当我滚动时,计时器的 UIlabel 总是重复。

就这样结束了。任何帮助将不胜感激。

http://gyazo.com/0d22d9e3adc0ecbbacd8cb2cdb59d8b4.png

这就是我的方法的样子

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

1 回答 1

2

明显的:

不要在(cell == nil)块外添加任何视图。

单元格被重复使用,这意味着如果您向重复使用的单元格添加任何标签,将有两个标签......如果再次重复使用该单元格,则会有更多标签。

还有许多其他有问题的部分。例如,从这个方法内部开始重新加载单元格是非常奇怪的。

此外,如果您使用 xib 进行单元设计,代码将大大简化。

于 2013-06-19T12:09:07.897 回答