ios - 当我运行应用程序时,我的 TableView 崩溃了

标签 ios uitableview crash parse-platform

我声明在我的应用中我使用 Parse.com 来存储和管理数据。

在我的表格 View 中,我引入了两个不同的自定义单元格。如果有图像,则显示 A,如果不存在图像,则显示另一只手。

现在一切似乎都正常,但是当我让应用程序运行时,Tableview 崩溃报告此错误:

2013-11-22 15:36:34.256 UniGo![17186:70b] -[UITableView _configureCellForDisplay:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:6246** 中断言失败

你能解释一下我错在哪里吗?

- (void)viewDidLoad  {
    [super viewDidLoad];
    self.FFTableView.delegate = self;
    self.FFTableView.dataSource = self;

    CellaSelezionata = -1;
}

-(void)viewDidAppear:(BOOL)animated {
    [self QueryForPost];
}

-(void)QueryForPost {

    PFQuery *QueryForFriend=[PFQuery queryWithClassName:FF_AMICIZIE_CLASS];
    [QueryForFriend whereKey:FF_AMICIZIE_A_USER equalTo:[PFUser currentUser]];
    [QueryForFriend whereKey:FF_AMICIZIE_STATO  equalTo:@"Confermato"];
    [QueryForFriend includeKey:FF_AMICIZIE_DA_USER];

    PFQuery *QueryYES = [PFQuery queryWithClassName:FF_POST_CLASS];
    [QueryYES whereKey:FF_POST_FLASH_POST_BOOLEANVALUE equalTo:[NSNumber numberWithBool:YES]];
    [QueryYES whereKey:FF_POST_SCELTI equalTo:[PFUser currentUser]];

    PFQuery *normalPostByFriends = [PFQuery queryWithClassName: FF_POST_CLASS];
    [normalPostByFriends whereKey: FF_POST_FLASH_POST_BOOLEANVALUE  equalTo: [NSNumber numberWithBool: NO]];
    [normalPostByFriends whereKey: FF_POST_UTENTE matchesKey:FF_AMICIZIE_DA_USER inQuery:QueryForFriend];

    PFQuery *normalPostByUser = [PFQuery queryWithClassName:FF_POST_CLASS];
    [normalPostByUser whereKey:FF_POST_FLASH_POST_BOOLEANVALUE  equalTo: [NSNumber numberWithBool: NO]];
    [normalPostByUser whereKey:FF_POST_UTENTE equalTo: [PFUser currentUser]];

    PFQuery *query = [PFQuery orQueryWithSubqueries:@[QueryYES,normalPostByFriends,normalPostByUser]];
    [query includeKey:FF_POST_UTENTE];
    [query orderByDescending:FF_CREATEDAT];
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {

        if (!error) {
            ArrayforPost = [[NSMutableArray alloc] init];
            for (PFObject *object in results) {
                [ArrayforPost addObject:object];
            }
            [self.FFTableView reloadData];
        }
    }];    

}


-(CGFloat)valoreAltezzaCella:(NSInteger)index {
    PFObject *objectPost = [ArrayforPost objectAtIndex:index];
    NSString *text = [objectPost objectForKey:@"Testo"];
    CGSize MAX = CGSizeMake(230, 10000);
    UIFont *FONT = [UIFont systemFontOfSize:14];
    CGSize altezzaLabel = [text boundingRectWithSize:MAX options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;
    return altezzaLabel.height;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [ArrayforPost count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    PFObject *ObjectPost = [ArrayforPost objectAtIndex:indexPath.row];

    // FIRST CUSTOM CELL
if (![ObjectPost objectForKey:FF_POST_IMMAGINE]) {

    static NSString *IdentificazioneCella = @"CellPost";

 FFCustomCellTimelineSocial * Cella = (FFCustomCellTimelineSocial *)[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCella];


    if (Cella == nil) {
        Cella = [[FFCustomCellTimelineSocial alloc]
                    initWithStyle:UITableViewCellStyleSubtitle
                    reuseIdentifier:IdentificazioneCella ];
    }

    Cella.backgroundCell.layer.cornerRadius = 2.0f;
    Cella.backgroundCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.backgroundCell.layer.borderWidth = 1.0f;
    Cella.backgroundCell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    Cella.BackgroundText.layer.cornerRadius = 2.0f;
    Cella.BackgroundText.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.BackgroundText.layer.borderWidth = 0.0f;
    Cella.BackgroundText.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    Cella.TimeBackground.layer.cornerRadius = 2.0f;
    Cella.TimeBackground.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.TimeBackground.layer.borderWidth = 1.0f;
    Cella.ViewTestataCell.layer.cornerRadius = 2.0f;
    Cella.ViewTestataCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    Cella.ViewTestataCell.layer.borderWidth = 1.0f;
    Cella.FFImmagineUtente.layer.masksToBounds = YES;
    Cella.FFImmagineUtente.layer.cornerRadius = 20.0f;
    Cella.FFImmagineUtente.contentMode = UIViewContentModeScaleAspectFill;

    Cella.FFTestoUtenteLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
    NSString *text = [ObjectPost objectForKey:FF_POST_TEXT];
    Cella.FFTestoUtenteLabel.text = text;
    [Cella.FFTestoUtenteLabel setLineBreakMode:NSLineBreakByTruncatingTail];

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd MMM"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    Cella.DataCorrente.text = dateString;

    NSString *NomeUser = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_NOMECOGNOME];
    Cella.FFNomeUtenteLabel.text = NomeUser;

    NSString *ImmagineUtente = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_FOTOPROFILO];
    Cella.FFImmagineUtente.file = (PFFile *)ImmagineUtente;
    [Cella.FFImmagineUtente loadInBackground];

    if (CellaSelezionata == indexPath.row) {
        CGFloat AltezzaLabel = [self valoreAltezzaCella: indexPath.row];
        Cella.FFTestoUtenteLabel.frame = CGRectMake(Cella.FFTestoUtenteLabel.frame.origin.x, Cella.FFTestoUtenteLabel.frame.origin.y, Cella.FFTestoUtenteLabel.frame.size.width, AltezzaLabel);  }

    else {

        Cella.FFTestoUtenteLabel.frame = CGRectMake(Cella.FFTestoUtenteLabel.frame.origin.x, Cella.FFTestoUtenteLabel.frame.origin.y, Cella.FFTestoUtenteLabel.frame.size.width, 55);
    }

    PFObject *rowObject = [ArrayforPost objectAtIndex:indexPath.row];
    if([[rowObject objectForKey:FF_POST_FLASH_POST_BOOLEANVALUE ] boolValue]) {
        Cella.FlashPostImg.image = [UIImage imageNamed:@"FFNotificaFlash"];
    }

    else {
        Cella.FlashPostImg.image =  [UIImage imageNamed:@" "]; }

    return Cella;
}


else {

        // SECOND CUSTOM CELL
    static NSString *IdentificazioneCellaIMG = @"CellIMG";

    FFCustomCellWithImage * CellaIMG = (FFCustomCellWithImage * )[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCellaIMG];

CellaIMG = (FFCustomCellWithImage * )[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCellaIMG];

    if (CellaIMG == nil) {
        CellaIMG = [[FFCustomCellWithImage alloc]
                  initWithStyle:UITableViewCellStyleSubtitle
                  reuseIdentifier:IdentificazioneCellaIMG ];
    }

    NSString *FotoPostSocial = [ObjectPost objectForKey:FF_POST_IMMAGINE];
    CellaIMG.FotoPost.file = (PFFile *)FotoPostSocial;
    [CellaIMG.FotoPost loadInBackground];

    CellaIMG.backgroundCell.layer.cornerRadius = 2.0f;
    CellaIMG.backgroundCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    CellaIMG.backgroundCell.layer.borderWidth = 1.0f;
    CellaIMG.backgroundCell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
      CellaIMG.TimeBackground.layer.cornerRadius = 2.0f;
    CellaIMG.TimeBackground.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    CellaIMG.TimeBackground.layer.borderWidth = 1.0f;
    CellaIMG.ViewTestataCell.layer.cornerRadius = 2.0f;
    CellaIMG.ViewTestataCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
    CellaIMG.ViewTestataCell.layer.borderWidth = 1.0f;
    CellaIMG.FotoProfilo.layer.masksToBounds = YES;
    CellaIMG.FotoProfilo.layer.cornerRadius = 20.0f;
    CellaIMG.FotoProfilo.contentMode = UIViewContentModeScaleAspectFill;

    CellaIMG.TestoPost.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
    NSString *text = [ObjectPost objectForKey:FF_POST_TEXT];
    CellaIMG.TestoPost.text = text;
    [CellaIMG.TestoPost setLineBreakMode:NSLineBreakByTruncatingTail];

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd MMM"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    CellaIMG.DataPost.text = dateString;

    NSString *NomeUser = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_NOMECOGNOME];
    CellaIMG.NomeUtente.text = NomeUser;

    NSString *ImmagineUtente = [[ObjectPost objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_FOTOPROFILO];
    CellaIMG.FotoProfilo.file = (PFFile *)ImmagineUtente;
    [CellaIMG.FotoProfilo loadInBackground];

    if (CellaSelezionata == indexPath.row) {
        CGFloat AltezzaLabel = [self valoreAltezzaCella: indexPath.row];
        CellaIMG.TestoPost.frame = CGRectMake(CellaIMG.TestoPost.frame.origin.x, CellaIMG.TestoPost.frame.origin.y, CellaIMG.TestoPost.frame.size.width, AltezzaLabel);
    }
    else {

        CellaIMG.TestoPost.frame = CGRectMake(CellaIMG.TestoPost.frame.origin.x, CellaIMG.TestoPost.frame.origin.y, CellaIMG.TestoPost.frame.size.width, 75);
    }

    return CellaIMG;

    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    if (CellaSelezionata == indexPath.row)  {

        //SPAZIO INTERNO VERSO IL BASSO QUANDO APRI LA CELLA
        return [self valoreAltezzaCella:indexPath.row] + 60 * 2;
    }

    else {

        //GRANDEZZA CELLA PRIMA DI APRIRE
        return 155 + 10 * 2;
    }

}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath    {
    if ([self valoreAltezzaCella:indexPath.row] > 65) {
        return indexPath;
    } else {
        return nil;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (CellaSelezionata == indexPath.row) {
        CellaSelezionata = -1;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        return;
    }

    if (CellaSelezionata >=  0) {
        NSIndexPath *previousPath = [NSIndexPath indexPathForRow:CellaSelezionata inSection:0];
        CellaSelezionata = indexPath.row;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];        
    }

    CellaSelezionata = indexPath.row;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

enter image description here

最佳答案

dequeueReusableCellWithIdentifier - can return you nil

如果发生这种情况,您需要检查并创建一个单元格:

FFCustomCellTimelineSocial * Cella = (FFCustomCellTimelineSocial *)[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCella];

if (Cella == nil) {
    Cella = [[[FFCustomCellTimelineSocial alloc] 
             initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier] autorelease];
}

关于ios - 当我运行应用程序时,我的 TableView 崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20147566/

相关文章:

iOS NSStream tcp server 客户端无法通信

javascript - 组合 Jquery 脚本崩溃页面

c# - .NET服务失败时,系统是否会记录异常?

ios - 外部页面转换不起作用 - "data-ajax=false"

ios - 错误 : "UILabel does not have a member named ' txt'.“

ios - 如何为核心数据模型对象自动生成默认的 UITableView?

ios - 无法在didSelectRowAtIndexPath中调用cellForRowAtIndexPath而不影响动画?

ios - Swift iOS 最佳实践 REST 回调

crash - 添加单元测试后,Xcode 8.3.1在启动时崩溃

ios - 让用户在 IOS 6 中选择谷歌地图和苹果 map ?