细胞显示从以前的细胞含量和某个崩溃

问题描述:

我有问题,一个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; 


} 

任何帮助吗?

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

+0

是的,它是UITableViewCell的子类,但我不做任何事情。什么是最好的方法 ?做一切在cellForRowAtIndexPath或重写在NewsQuoteCell prepareForReuse?顺便提一下,我有5种不同的自定义单元格(报价,文章,视频,音频和图片)。我必须创建一个subclasse foreach或在cellForRowAtIndexPath中编码所有内容? – Abdessamad 2012-07-29 12:03:56

+0

我仍然需要解决第一个问题。我注意到,只有cellText和cellMedia(用于图片)使用旧内容而不是其他内容。 – Abdessamad 2012-07-29 12:08:48

+0

我发现这个问题,它是由于这行[cellText setTag:indexPath.row] .... – Abdessamad 2012-07-29 12:35:23

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

你可能正在做一些有趣的标签。您是否正确设置了标签?一个确定的问题是你的第二个错误信息。您正在使用viewWithTag调用检索UIImageView,并使用UITextView来引用它。确保标签设置正确。它不会伤害到标签值设置为独特的东西,如101,102,103等。

希望这会有所帮助。

+0

我不知道为什么,但将故事板中的标记从5更改为105修复了这个问题......怪异...标记从一开始就引用了文本视图,这就是为什么我不明白为什么我的应用程序崩溃。 – Abdessamad 2012-07-29 12:06:20

+0

但我仍然需要解决第一个问题。我注意到,只有cellText和cellMedia(用于图片)使用旧内容而不是其他内容。 – Abdessamad 2012-07-29 12:08:05

+0

我发现这个问题,这是由于这行[cellText setTag:indexPath.row] .... – Abdessamad 2012-07-29 12:35:07