带有两个自定义单元格的UITableView(多个标识符)

Moh*_*lah 29 objective-c uitableview ios

我试图将一个单元格作为每个单元格之间的空格 - 这将通过设置alpha = 0来隐藏.在我的表格中,空格单元格将用于奇数行.

请注意,实际的单元格高度为85,但隐藏的单元格高度(即单元格之间的空间)为20.

问题是空间单元高度是85,但不是20,我不知道为什么.可能是单元格未正确加载.

Cell这里是UITableViewCell- 实际的单元格 - 标识符为'Cell'.

Cell2 是标识符"Space"的空间.

上面的每个类都有自己的UITableViewCell类,并且XIB文件也分配给它们.该标识符也被在IB为每个XIB设置.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"Cell";
static NSString *CellIdentifier2 = @"Space";

Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(!cell)
{
    NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];
    for (id obj in ar)
    {
        if ([obj isKindOfClass:[Cell class]])
        {
            cell = (Cell *)obj;
            break;
        }
    }
}

if (indexPath.row % 2 == 1)
{
    Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if (!cell2)
    {
        NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil];
        for(id obj in ar)
        {
            if([obj isKindOfClass:[Cell2 class]])
            {
                cell2 = (Cell2 *)obj;
                break;
            }
        }

        // Method 1
        cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
         // Method 2
        //cell2 = [[Cell2 alloc] init];
        // Method 3
        //cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        [cell2.contentView setAlpha:0];
        // prevent selection and other stuff
        [cell2 setUserInteractionEnabled:NO];
    }
    return cell2;
}
else
{
    // Configure the actual cell
}


return cell;
Run Code Online (Sandbox Code Playgroud)

}

Att*_*a H 59

*为了更好地理解,我已经重命名了一些你的NIB/Class名称.*

首先,您应该注册每个单元格的NIB:

- (void)viewDidLoad{
    [super viewDidLoad];

    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];

    nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];

    self.contentView.hidden = YES;
    [self loadData];
}
Run Code Online (Sandbox Code Playgroud)

因为您已注册NIB,所以dequeueReusableCellWithIdentifier:将始终返回一个单元格:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    // Space Cell
    if (indexPath.row % 2 == 1) {
        CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        return cell;
    }

    // Content cell
    else {
        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        // Configure cell
        return cell;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,但并非最不重要的是,请确保实现以下委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Space cell's height
    if (indexPath.row % 2 == 1) {
        return 20.0f;
    }

    // Content cell's height
    else {
        return 80.0f;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 10

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *returncell;

    static NSString *cellIdentifier ;
    if(indexPath.section == 0)
    {
        cellIdentifier = @"cell1";
    }
    else if (indexPath.section == 1)
    {
        cellIdentifier = @"cell2";
    }
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(indexPath.section == 0)
    {
        if(myCell == nil)
        {
            myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30);
            [getLocationBtn setTitle:@"your button title" forState:UIControlStateNormal];
            [getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [getLocationBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

        }

        [myCell.contentView addSubview:getLocationBtn];
        returncell = myCell;
    }
    else if (indexPath.section == 1)
    {
        if (myCustomCell == nil)
        {
            myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        myCustomCell.nearbyLocation.text = @"demo Text";
        returncell = myCustomCell;
    }

    return returncell;
}
Run Code Online (Sandbox Code Playgroud)

// mycustom tablviewcell

导入"MapTableViewCell.h"

@implementation MapTableViewCell

@synthesize nearbyLocation;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {    
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];

        [self addSubview:nearbyLocation];
    }

    return self;
}
@end
Run Code Online (Sandbox Code Playgroud)

使用默认单元格的自定义单元格数量的最佳方法