1

I have a UITableViewController with static cells in an App. Is there any way I can use default cells in the table view along with subclass cells by code? My tableview has 8 rows and 6 of those rows want to use default cells in the tableview. For the remaining two cells I want to create it by code.

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

    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];

    if (cell == nil) {
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"];
    }

    return cell;
}

And in the MyCustomCell.m contains,

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) {
    // Initialization code
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    self.myLabel.backgroundColor = [UIColor clearColor];
    self.myLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    self.myLabel.text = @"Hi there";
    [self.contentView addSubview:self.myLabel];
}
return self;
}

The -tableView:CellForRowAtIndexPath: method helps to create the custom cells by code but I have no idea idea how to access the default cells here if it is possible.

Sibin
  • 133
  • 2
  • 8
  • Why don't you do it in storyboard? – Sviatoslav Yakymiv Jul 22 '14 at 13:07
  • Actual purpose of this is implementing In-line picker cells. Loading more pickers (in my case 4 pickers) from storyboard delays loading the view (3-4 seconds). I don't want to do that. – Sibin Jul 22 '14 at 13:20
  • That's impossible loading 2 cells from Storyboard takes 3-4 seconds. If you really want to avoid Storyboards you can place them in xib files. – Szu Jul 22 '14 at 13:22
  • [iOS 7 slow to open UITableViewController with UIPickerView](http://stackoverflow.com/questions/19239003/ios-7-slow-to-open-uitableviewcontroller-with-uipickerview/22313997) I tested it on iPhone 4 and it is kind of slow. – Sibin Jul 22 '14 at 13:38

2 Answers2

1

You can use the indexPath.row property to see if the row you are targeting is less than row 6 and if that's the case then dequeue a cell that isn't your custom cell.

First create your custom cell and give it an identifier ("myCustomCellReuseId). Then in your view controller use:

[tableview registerNib:[UINib nibWithName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCustomCellReuseId"];

Then, in your prototype cells in Storyboard, give the default cells an identifier different than the one you gave your custom cell.

Then, in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath use:

if(indexPath.row > 5) {
   CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCellReuseId"];
} else {
  //
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

}
KerrM
  • 5,139
  • 3
  • 35
  • 60
  • I'm not sure how to do that. In my case, first 6 rows must be the defaults cell from storyboard and the rest 2 by the code. Is it possible? If so could you please provide the code. Thanks. – Sibin Jul 22 '14 at 13:14
  • I know how to do it using Dynamic cell type. I was trying use all Static cells for this which saves me from writing lot of code. From all the responses I assume it is not possible to use Static cells in this case. I think my question was not clear :P . Thanks for the help any way. – Sibin Jul 22 '14 at 13:36
1

As @Sviatoslav Yakymiv mentioned the easiest way to design your cell is to mix Storyboards with programically customization: the layout you will be done in Storyboard but the content you will change in your view controller .m file. That mean all code you have in -initWithStyle: reuseIdentifier: can be designed in IB. Then you can create 2 defferent dynamic prototypes in IB. In other words you can mix your custom cells with default UITableViewCell. For example in Interface Builder you have dynamic prototype cell:

  1. Default UITableViewCell with reuseIdentifier=@"Cell".

  2. Custom cell (you can change it in Indentity Inspector on the right top) with reuseIdentifier=@"MyCustomCell".

If you will do that correctly you will no logen need to use these 3 lines:

    if (cell == nil) {
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"];
    }

Then you should change your function to:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row >= rowWhereCustomCellShouldShow && indexPath.section > yourSection) {
        MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
        [cell customizeForData:data[indexPath.row];
        return cell;
    } else { // The apple default cell
        UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        // Here you can customize your cell.
        return cell2;
    }
}
Szu
  • 2,254
  • 1
  • 21
  • 37
  • I know how to do it using Dynamic cell type. I was trying use all Static cells for this case to avoid extra code. From all the responses I assume it is not possible to use Static cells in this case. Thanks for the help. – Sibin Jul 22 '14 at 13:42