2012-07-29 71 views
0

我有問題,一個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; 


} 

任何幫助嗎?

回答

0

它看起來像你有一個叫做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

0

我假設你在一個單獨的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