32

I am trying to put a cell as a space between each cell - which will be hidden by setting alpha = 0. In my table, the space cells will be for rows that are odd.

Note that the actual cell height is 85, but the hidden cell height (ie space between cells) is 20.

The problem is that the space cell height is 85, but not 20, I don't know why. Maybe the cell is not loaded correctly.

Cell here is the UITableViewCell - the actual cell - with identifier 'Cell'.

Cell2 is the space with identifier 'Space'.

Each class above has its own UITableViewCell class and the XIB files are also assigned to each of them. The identifier is also set in the IB for each 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;

}

shim
  • 9,289
  • 12
  • 69
  • 108
MohamMad Salah
  • 971
  • 2
  • 14
  • 31
  • If your goal is just to add spacing, you can do it my making each row a section. See [this answer](http://stackoverflow.com/a/33931591). – Suragch Aug 16 '16 at 17:42

3 Answers3

60

* I've renamed some of your NIB/Class names for a better understanding. *

First, you should register each cells' 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];
}

Because you have the NIBs registered, dequeueReusableCellWithIdentifier: will always return a cell:

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

Last, but not least, make sure to implement the following delegate method:

- (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;
    }
}
Attila H
  • 3,616
  • 2
  • 24
  • 37
  • I have tried your and change the cells ids and nib names to match the ones in my code, but I got this error: '*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (Cell) - nib must contain exactly one top level object which must be a UITableViewCell instance' ***' – MohamMad Salah Jan 14 '13 at 08:40
  • A NIB file for custom UITableViewCell should contain exactly one top level view. – Attila H Jan 14 '13 at 18:14
  • Thanks for your responding,,, but I don't know what 'one top level view' means? – MohamMad Salah Jan 15 '13 at 06:34
  • It means you should have a (separate) XIB for each custom UITableViewCell. – Attila H Jan 15 '13 at 06:41
  • Oh I have already done this ,,, CellSpace has its own XIB also CellView as well – MohamMad Salah Jan 15 '13 at 06:43
  • Make sure you only have a UITableViewCell in your .xib. The ONLY other views should be subviews of a single UITableViewCell view. Check that your subviews are actually subviews of the UITableViewCell and not floating above or behind the cell. – LightningStryk May 23 '13 at 22:11
  • Your TableViewCell must be the main view and all other should be subviews – Usman Awan Jul 26 '13 at 04:51
  • Once I got the same problem as MohanMad Salah has and tried to solve it by all possible ways, which includes answers of Attila H and Usman Awan, though I failed. Then, I didn't have any other option other than recreating custom cell again. So, I deleted and recreated custom cell and then it worked fine. I don't know what was the problem, but issue may be related to xcode/xib or I might have missed something. – Nitesh Borad Apr 28 '15 at 12:24
  • Great! solution for me. – Kernelzero Jul 07 '16 at 07:19
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;
}

//mycustom tablviewcell

import "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

Best way to use number of custom cells with default cell

Community
  • 1
  • 1
Sandeep Jangir
  • 412
  • 5
  • 14
0

In addition for the answers provided, I want to emphasize on the Cell Identifier for each different custom cells must be different too.

For example custom cellA with identifier "Cell" and custom cellB with identifier "Cell2".

Dan
  • 810
  • 2
  • 11
  • 29