我有問題,一個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;
}
任何幫助嗎?
是的,它是UITableViewCell的子類,但我不做任何事情。什麼是最好的方法 ?做一切在cellForRowAtIndexPath或重寫在NewsQuoteCell prepareForReuse?順便提一下,我有5種不同的自定義單元格(報價,文章,視頻,音頻和圖片)。我必須創建一個subclasse foreach或在cellForRowAtIndexPath中編碼所有內容? – Abdessamad 2012-07-29 12:03:56
我仍然需要解決第一個問題。我注意到,只有cellText和cellMedia(用於圖片)使用舊內容而不是其他內容。 – Abdessamad 2012-07-29 12:08:48
我發現這個問題,它是由於這行[cellText setTag:indexPath.row] .... – Abdessamad 2012-07-29 12:35:23