【问题标题】:My TableView crashes when I make the run App当我制作运行应用程序时,我的 TableView 崩溃了
【发布时间】:2013-11-22 14:43:55
【问题描述】:

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

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

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

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

你能解释我哪里错了吗?

- (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];
}

【问题讨论】:

  • 您可以发布崩溃时显示的完整消息吗?在某些情况下,您很可能会返回 nil 而不是单元格。
  • 您遗漏了错误消息的重要部分。更新您的问题(不要在评论中发表)其余错误。

标签: ios uitableview crash parse-platform


【解决方案1】:
dequeueReusableCellWithIdentifier - can return you nil

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

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

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

【讨论】:

  • 我根据您的建议编辑了我的代码.. 现在它可以工作了!应用程序没有崩溃,但没有显示第二个单元格...不显示第二个单元格的内容,您只看到白色单元格但没有内容...我哪里出错了?
  • 您希望它是什么类型的单元格?
  • 我插入了 Tableview 的图像,以便您更好地理解...您可以看到第一个单元格显示完美,但是第二个单元格完全是白色的...第二个单元格的内容是相同的除了第一个单元格的内容之外,第一个单元格的内容也有一个 UIImageView
  • 您是否在委托中返回正确数量的单元格?如果它要求此单元格,请尝试调试。在 cellForRowAtIndexPath 中放置一些 NSLog
  • 我刚刚解决了......对不起,这是我的愚蠢错误......它'令人惊讶的是,一个小错误会让你每次两个小时都失去愚蠢的解决方案:(缺少一个“ A" nell'identifier:(我写的是 CellIMG 而不是 CellaIMG ......太棒了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
相关资源
最近更新 更多