您的位置:首页 > 产品设计 > UI/UE

iOS 在一个UITableview中添加多个UIViewCell

2015-06-12 14:55 393 查看
1.我们会碰到这种情况,在一个tableview中,我们可能需要每个Cell的内容和样式呈现的效果不一样,此时我们可能需要根据要求去建立不同的Cell,因此涉及到在一个tableview中填充多个不同的Cell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    static NSString *CellIdentify = @"CellIdentify";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentify];//

    Model *model = [Array objectAtIndex:indexPath.row];//先获取要填充进Cell的数据模型

    if ([model.condition isEqualToString:@"condition1"]) {
        cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:@"TableViewCell1"];

        if (!cell) {
            NSArray *nib =
            [[NSBundle mainBundle] loadNibNamed:@"TableViewCell1" owner:self options:nil];
            cell = [nib objectAtIndex:0];//读取第一个你要填充的Cell的xib文件
        }

    }

    else {
        cell = (TableViewCell2*)[tableView dequeueReusableCellWithIdentifier:@"TableViewCell2"];
        if (!cell) {
            NSArray *nib =
            [[NSBundle mainBundle] loadNibNamed:@"TableViewCell2" owner:self options:nil];//读取第二个Cell的xib
            cell = [nib objectAtIndex:0]
        }
    }

    [cell fillCellWithObject:model];//将内容填充进Cell中

    
    return cell;
}
2.返回自适应Cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

   
    return cell.frame.size.height;
   
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息