我遇到了 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;
}
请问有什么帮助吗?