滚动时UITableViewCells显示错误

时间:2014-09-17 19:01:23

标签: ios objective-c uitableview

我有一个简单的博客应用程序,它将主屏幕上的文章列为UITableView。第一次加载时一切看起来都很好,但是当我向下滚动并向后滚动时,它会重新绘制单元格错误,并且......好吧,看看这张照片。 enter image description here

下面列出了我用于细胞的代码。我做错了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";  

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {  
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }



    RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
    NSString *substring = @"http://316apps.com/ipreachersblog/wp";
    NSRange textRange = [entry.articleImage rangeOfString:substring];
    NSString *substring2 = @"http://ipreacher.files.wordpress.com";
    NSRange textRange2 = [entry.articleImage rangeOfString:substring2];


    if(textRange.location != NSNotFound){
        NSString *thearticleImage = entry.articleImage;
        NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\"" options:NSRegularExpressionCaseInsensitive error:NULL];
        NSString *someString = thearticleImage;
        NSString *oneurl = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
        NSString *finalstring = [oneurl stringByReplacingOccurrencesOfString:@"src=\"" withString:@""];
        NSString *thefinalstring = [finalstring stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterShortStyle];

        NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
        UIFont *cellFont = [UIFont fontWithName:@"Copperplate-Bold" size:21];

        UIFont *cellFont2 = [UIFont fontWithName:@"Copperplate-Bold" size:18];
        CALayer * l = [cell.imageView layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:30];
        [l setBorderWidth:2.0];
        [l setBorderColor:[[UIColor blackColor] CGColor]];
        cell.textLabel.text = entry.articleTitle;
        cell.textLabel.numberOfLines = 3;
        cell.detailTextLabel.text = articleDateString;
        cell.textLabel.font = cellFont;
        cell.detailTextLabel.font = cellFont2;
        [cell.imageView setImageWithURL:[NSURL URLWithString:thefinalstring] placeholderImage:[UIImage imageNamed:@"Icon-60@2x.png"]];
        UIColor *bobcatred = UIColorFromRGB(0x841617);
        cell.textLabel.textColor = bobcatred;

        cell.backgroundColor = [UIColor lightGrayColor];


    }
    else if (textRange2.location != NSNotFound){
        NSString *thearticleImage = entry.articleImage;
        NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\"" options:NSRegularExpressionCaseInsensitive error:NULL];
        NSString *someString = thearticleImage;
        NSString *oneurl = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
        NSString *finalstring = [oneurl stringByReplacingOccurrencesOfString:@"src=\"" withString:@""];
        NSString *thefinalstring = [finalstring stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterShortStyle];

        NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
        UIFont *cellFont = [UIFont fontWithName:@"Copperplate-Bold" size:21];

        UIFont *cellFont2 = [UIFont fontWithName:@"Copperplate-Bold" size:18];
        CALayer * l = [cell.imageView layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:30];
        [l setBorderWidth:2.0];
        [l setBorderColor:[[UIColor blackColor] CGColor]];
        cell.textLabel.text = entry.articleTitle;
        cell.textLabel.numberOfLines = 3;
        cell.detailTextLabel.text = articleDateString;
        cell.textLabel.font = cellFont;
        cell.detailTextLabel.font = cellFont2;
        [cell.imageView setImageWithURL:[NSURL URLWithString:thefinalstring] placeholderImage:[UIImage imageNamed:@"Icon-60@2x.png"]];
        UIColor *bobcatred = UIColorFromRGB(0x841617);
        cell.textLabel.textColor = bobcatred;

        cell.backgroundColor = [UIColor lightGrayColor];


    }

    else    {
        CALayer * l = [cell.imageView layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:30];
        [l setBorderWidth:2.0];
        [l setBorderColor:[[UIColor blackColor] CGColor]];
        NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterShortStyle];
        NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
        UIFont *cellFont = [UIFont fontWithName:@"Copperplate-Bold" size:21];
        UIFont *cellFont2 = [UIFont fontWithName:@"Copperplate-Bold" size:18];
        cell.imageView.image = [UIImage imageNamed:@"Icon-60@2x.png"];
        cell.textLabel.text = entry.articleTitle; 
        cell.detailTextLabel.text = articleDateString;
        cell.textLabel.font = cellFont;
        cell.detailTextLabel.font = cellFont2;
        UIColor *bobcatred = UIColorFromRGB(0x841617);
        cell.textLabel.textColor = bobcatred;
        cell.textLabel.numberOfLines = 3;

        cell.backgroundColor = [UIColor lightGrayColor];


    }


    return cell;
}

单元格是自定义的,其实现是:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(1,6,60,60);
    float limgW =  self.imageView.image.size.width;
    if(limgW > 0) {
        self.textLabel.frame = CGRectMake(74,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(74,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width,self.detailTextLabel.frame.size.height);
    }
}
@end

0 个答案:

没有答案