2014-01-30 49 views
0

我使用这个解决方案作为我的tableview,我的单元格只包含文本和单元格,还有一个图像,所以我想用一个解决方案与故事板和动态单元格来布局不同的单元格和与此代码更改CellIdentifier使用具有不同布局的多个动态单元格

NSString *CellIdentifier = @"Cell"; 
NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row]; 
if ([[object objectForKey:@"type"] isEqualToString:@"text"]){ 
    CellIdentifier = @"OnlyTextCell"; 
}else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){ 
    CellIdentifier = @"ImageTextCell"; 
} 

难道我已经使用不同的UITableViewCells,如: - OnlyTextCell - ImageTextCell

OnlyTextCell *cell = (OnlyTextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
ImageTextCell *cell = (ImageTextCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

然后return cell

东西丢失在这里,因为我也知道,*细胞两次是不工作...

所以我得到这样的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 

    TTTTimeIntervalFormatter *timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init]; 
    NSString *timestamp = [timeIntervalFormatter stringForTimeInterval:[[object objectForKey:@"datum"] timeIntervalSinceNow]]; 


    if ([[object objectForKey:@"type"] isEqualToString:@"text"]){ 
     ChatCell *cell = (ChatCell *)[tableView dequeueReusableCellWithIdentifier:@"ChatCell"]; 
     // Send the text over to the cell. 
     NSLog(@"ChatCell"); 

     cell.naamGebruiker.text = [[object objectForKey:@"fromUser"] objectForKey:@"accountName"]; 
     cell.timeLabel.text = timestamp; 

     cell.gebruikerImage.layer.masksToBounds = YES; 
     PFFile *thumbnail = [[object objectForKey:@"fromUser"] objectForKey:@"facebookImage"]; 
     cell.gebruikerImage.image = [UIImage imageNamed:@"no-image"]; 
     cell.gebruikerImage.file = thumbnail; 

     [cell.gebruikerImage loadInBackground]; 

     cell.gebruikerImage.layer.cornerRadius = cell.gebruikerImage.frame.size.height /2; 
     cell.gebruikerImage.layer.masksToBounds = YES; 
     cell.gebruikerImage.layer.borderWidth = 0; 

     cell.chatBerichtTekst.text = [object objectForKey:@"tekst"]; 
     cell.chatBerichtTekst.numberOfLines = 0; 
     [cell.chatBerichtTekst setLineBreakMode:NSLineBreakByWordWrapping]; 
     [cell sizeToFit]; 

     return cell; 

    } else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){ 
     ChatImageCell *cell = (ChatImageCell *)[tableView dequeueReusableCellWithIdentifier:@"ChatImageCell"]; 
     // Send the image over the cell. 
     NSLog(@"ChatImageCell"); 

     cell.naamGebruiker.text = [[object objectForKey:@"fromUser"] objectForKey:@"accountName"]; 
     cell.timeLabel.text = timestamp; 

     cell.gebruikerImage.layer.masksToBounds = YES; 
     PFFile *thumbnail = [[object objectForKey:@"fromUser"] objectForKey:@"facebookImage"]; 
     cell.gebruikerImage.image = [UIImage imageNamed:@"no-image"]; 
     cell.gebruikerImage.file = thumbnail; 

     [cell.gebruikerImage loadInBackground]; 

     cell.gebruikerImage.layer.cornerRadius = cell.gebruikerImage.frame.size.height /2; 
     cell.gebruikerImage.layer.masksToBounds = YES; 
     cell.gebruikerImage.layer.borderWidth = 0; 

     cell.chatBerichtTekst.text = [object objectForKey:@"tekst"]; 
     cell.chatBerichtTekst.numberOfLines = 0; 
     [cell.chatBerichtTekst setLineBreakMode:NSLineBreakByWordWrapping]; 
     [cell sizeToFit]; 

     return cell; 
    } 

    return nil; 


} 

回答

2

确保您设置单元格将Storyboard的每个动态单元格中的标识符分配给正确的OnlyTextCell和ImageTextCell。然后在您的tableView:的cellForRowAtIndexPath:你应该有这样的事情......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSDictionary *object = [self.listOfStuff objectAtIndex:indexPath.row]; 
    if ([[object objectForKey:@"type"] isEqualToString:@"text"]){ 
     OnlyTextCell *cell = (OnlyTextCell *)[tableView dequeueReusableCellWithIdentifier:@"OnlyTextCell"]; 
     // Send the text over to the cell. 
     return cell; 
    }else if ([[object objectForKey:@"type"] isEqualToString:@"image"]){ 
     ImageTextCell *cell = (ImageTextCell *)[tableView dequeueReusableCellWithIdentifier:@"ImageTextCell"]; 
     // Send the image over the cell. 
     return cell; 
    } 
    return nil; 
} 
+0

日Thnx,我在我的问题上面编辑完整的代码。错误是在我得到的数据中,我想... –

相关问题