当我在我的表格视图中滚动时,单元格似乎正在刷新,因为左角的持续时间有时会显示,有时不会。有谁知道我怎样才能让它留在一个地方?
- (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;
}
编辑:
关注你们之后发生了这件事
我尝试使用您的解决方案,并以这段代码结束,但是当我向下滚动时,一切都恢复正常。有任何想法吗 ?
- (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;
}