使用具有不同布局的多个动态单元格

时间:2014-01-30 21:43:16

标签: objective-c uitableview uistoryboard

我在桌面视图中使用这个解决方案,我的单元格只包含文本和单元格以及图像,因此我想使用带有故事板和动态单元格的解决方案来布局不同的单元格,并使用此代码更改CellIdentifier < / p>

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;


}

1 个答案:

答案 0 :(得分: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;
}