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;


}

请问有什么帮助吗?

4

2 回答 2

0

I assume you have created NewsQuoteCell in a seperate nib file. If thats the case why dont you setup proper IBOutlets to bind different components like the cellLabel, cellDate, cellText etc. That way you don't have to retrieve these components using viewWithTag.

And you are probably doing something funny with the tag. Have you setup the tags properly? One definite problem is your second error message. You are retrieving a UIImageView with the viewWithTag call and referencing it with UITextView. Make sure that tags are set up properly. And it never hurts to set the tag values to something unique, like 101, 102,103... etc.

Hope this helps.

于 2012-07-29T03:58:53.913 回答
0

It looks like you have a subclass of UITableViewCell called NewsQuoteCell. You can override the prepareForReuse method in the NewsQuoteCell class to fix your new cell shows content of previous cell issue. The other issue seemed to have to do with the your tag for view number 5 was not a UITextView but a UIImageView. I would double check your NewsQuoteCell's implementation file.

于 2012-07-29T04:03:35.190 回答