UITableviewに問題があります。データを取得してカスタムセルビューに表示しています。2つの問題を除いて、ほとんどすべてがうまくいっています。
- 下にスクロールすると、いくつかの新しいセルに前のセルのコンテンツが表示されます(特にTextView cellText) 。
- アプリが[UIImageViewsetText:]でクラッシュすることがあります:認識されないセレクターが次の行でインスタンスに送信されました:[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;
}
何か助けてください?