单元格显示先前单元格中的内容,有时会崩溃

时间:2012-07-29 03:33:48

标签: ios uitableview instance

我对UITableview有疑问,我正在获取一些数据并在自定义单元格视图中显示它,除了两个问题外,几乎一切都很顺利:

  • 当我向下滚动时,一些新单元格会显示以前单元格中的内容(尤其是 TextView cellText
  • 有时应用程序崩溃 [UIImageView setText:]:无法识别的选择器发送到实例的行: [cellText setText:postText];

这是我的cellForRowAtIndexPath方法:

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


NSString *PostCellIdentifier;

Post *info = [posts objectAtIndex:indexPath.row];
NSString *PostType = info.type;

if([PostType isEqualToString:@"quote"]) {

    PostCellIdentifier = @"NewsQuoteCell";

} else if([PostType isEqualToString:@"article"]) {

    PostCellIdentifier = @"NewsArticleCell";

} else if([PostType isEqualToString:@"picture"]) {

    PostCellIdentifier = @"NewsPictureCell";

} else if([PostType isEqualToString:@"video"]) {

    PostCellIdentifier = @"NewsVideoCell";

} else if([PostType isEqualToString:@"audio"]) {

    PostCellIdentifier = @"NewsAudioCell";

}    


NewsQuoteCell *cell = [tableView dequeueReusableCellWithIdentifier:PostCellIdentifier];

if (cell == nil) {
    cell = [[NewsQuoteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PostCellIdentifier];
}



UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:1];
[cellLabel setText:info.masjid_name];

UILabel *cellDate = (UILabel *)[cell.contentView viewWithTag:2];
[cellDate setText:info.date];


UITextView *cellText = (UITextView *)[cell.contentView viewWithTag:5];
NSString *postText = info.title;
[postText stringByDecodingHTMLEntities];    
if ([postText length] > 300) { 
    postText = [postText substringToIndex:300];
    postText = [postText stringByAppendingString:@"..."];
}
[cellText setText:postText];

CGRect frame = cellText.frame;
frame.size.height = cellText.contentSize.height;
cellText.frame = frame;


UIImageView *cellImage = (UIImageView *)[cell.contentView viewWithTag:3];
NSString *imagePath = info.masjid_thumb;
[cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", imagePath]]];

cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"news_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 10, 0)] ];  
cell.selectedBackgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"news_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 10, 0)] ];






if([PostType isEqualToString:@"article"]) {


    cellText.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgrText = [[UITapGestureRecognizer alloc] 
                                       initWithTarget:self action:@selector(handleTapArticle:)];
    [cellText setTag:indexPath.row];
    [cellText addGestureRecognizer:pgrText];



    UIImageView *cellMedia = (UIImageView *)[cell.contentView viewWithTag:10];

    cellMedia.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgr = [[UITapGestureRecognizer alloc] 
                                   initWithTarget:self action:@selector(handleTapArticle:)];
    [cellMedia setTag:indexPath.row];
    [cellMedia addGestureRecognizer:pgr];

} else if ([PostType isEqualToString:@"video"]) {


    cellText.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgrText = [[UITapGestureRecognizer alloc] 
                                       initWithTarget:self action:@selector(handleTap:)];
    [cellText setTag:indexPath.row];
    [cellText addGestureRecognizer:pgrText];



    UIImageView *cellMedia = (UIImageView *)[cell.contentView viewWithTag:10];

    cellMedia.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgr = [[UITapGestureRecognizer alloc] 
                                   initWithTarget:self action:@selector(handleTap:)];
    [cellMedia setTag:indexPath.row];
    [cellMedia addGestureRecognizer:pgr];


} else if ([PostType isEqualToString:@"audio"]) {


    cellText.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgrText = [[UITapGestureRecognizer alloc] 
                                       initWithTarget:self action:@selector(handleTap:)];
    [cellText setTag:indexPath.row];
    [cellText addGestureRecognizer:pgrText];


    UIImageView *cellMedia = (UIImageView *)[cell.contentView viewWithTag:10];

    cellMedia.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgr = [[UITapGestureRecognizer alloc] 
                                   initWithTarget:self action:@selector(handleTap:)];
    [cellMedia setTag:indexPath.row];
    [cellMedia addGestureRecognizer:pgr];


} else if ([PostType isEqualToString:@"picture"]) {

    cellText.layer.shadowColor = [[UIColor blackColor] CGColor];
    cellText.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    cellText.layer.shadowOpacity = 0.5f;
    cellText.layer.shadowRadius = 0.5f;

    UIImageView *cellMedia = (UIImageView *)[cell.contentView viewWithTag:7];

    NSString *mediaPath = info.media;


   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:mediaPath]];
    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {

        UIImage* scaled2 = [image scaleToFitSize:(CGSize){284, 284}];
        [cellMedia setImage:scaled2];

        CGRect frame1 = cellMedia.frame;
        frame1.size.width = 284;
        frame1.size.height = scaled2.size.height;
        cellMedia.frame = frame1;

    }];
    [operation start];



    cellMedia.userInteractionEnabled = YES;
    UITapGestureRecognizer *pgr = [[UITapGestureRecognizer alloc] 
                                   initWithTarget:self action:@selector(handleTapPicture:)];
    [cellMedia setTag:indexPath.row];
    [cellMedia addGestureRecognizer:pgr];




    if ([postText length] > 300) { 
        postText = [postText substringToIndex:300];
        postText = [postText stringByAppendingString:@"..."];
    }
    [cellText setText:postText];

    CGRect frame = cellText.frame;

    frame.size.height = cellText.contentSize.height;
    frame.origin.y = cellMedia.frame.origin.y + (cellMedia.frame.size.height - cellText.contentSize.height);
   cellText.frame = frame;

}


return cell;


}

请帮忙吗?

2 个答案:

答案 0 :(得分:0)

我假设您已在单独的nib文件中创建了NewsQuoteCell。如果是这样的话,为什么不设置正确的IBOutlet来绑定不同的组件,如cellLabel,cellDate,cellText等。这样你就不必使用viewWithTag来检索这些组件。

你可能正在用标签做一些有趣的事情。您是否正确设置了标签?一个明确的问题是你的第二个错误信息。您正在通过UIImageView来电检索viewWithTag并使用UITextView引用该{{1}}。确保标记设置正确。将标签值设置为独特的东西(例如101,102,103 ......等)永远不会受到伤害。

希望这有帮助。

答案 1 :(得分:0)

看起来你有一个名为NewsQuoteCell的UITableViewCell的子类。您可以覆盖NewsQuoteCell类中的prepareForReuse方法,以修复新单元格显示上一个单元格问题的内容。另一个问题似乎与视图号5的标签有关,不是UITextView而是UIImageView。我会仔细检查你的NewsQuoteCell的实现文件。