0

I want to make an option for my users that they can choose what kind of list in the UITableView they want.

In my UITableView, I have made 2 prototype cells. The user can switch between these 2 cell types. But the cell won't change his size;

Before switch: http://imgbox.com/kMQOiSeI
After switch: http://imgbox.com/tEBBla81

Now I switch between these in the cellForRowIndexPath;

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *voorkeur = [[NSUserDefaults standardUserDefaults] objectForKey:@"voorkeur"];
        static NSString *CellIdentifier;

        if ([voorkeur isEqualToString:@"groot"]) {
            CellIdentifier = @"CustomCell2";
        } else {
            CellIdentifier = @"CustomCell";
        }

        CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (Cell == nil) {
            Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
(...)

    }

What can I do to make this work?

Thanks!

Rick
  • 33
  • 7
  • You should use `dequeReusableCellWithIdentifier:forIndexPath:`. That should also never return nil, unless the reuse identifier is not registered. Are you using autolayout or are you defining the height in `heightForRowAtIndexPath`? – Rory McKinnel May 12 '15 at 20:59
  • @RoryMcKinnel Hi, I use auto layout. Can you give an example of your answer? :) – Rick May 12 '15 at 21:29
  • `CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];`. – Rory McKinnel May 12 '15 at 21:41
  • Remove `if (Cell == nil)` code as it should never return nil unless your cell identifiers are not registered properly. – Rory McKinnel May 12 '15 at 21:44
  • Check all your constraints are correct on both cell types. – Rory McKinnel May 12 '15 at 21:46
  • @RoryMcKinnel Hmm... Adding forIndexPath: makes it even worser... By the way; I'm not using auto layout. Sorry, mistake... – Rick May 12 '15 at 22:07
  • If not using auto layout, what do you return for `heightForRowAtIndexPath`?Or have you set `self.tableView.rowHeight = UITableViewAutomaticDimension` in `viewWillAppear`? – Rory McKinnel May 12 '15 at 22:08
  • @RoryMcKinnel Nothing, i don't have that method.. – Rick May 12 '15 at 22:13
  • You either need to implement `heightForRowAtIndexPath` and return the height for the cell or if only using iOS8, you can use the automatic cell sizing. The latter requires you to NOT have `heightForRowAtIndexPath` implemented and `self.tableView.tableHeight = UITableViewAutomaticDimension` set. You also need to implement `estimatedHeightForRowAtIndexPath:` or set `self.tableView.estimatedRowHeight` to a value. – Rory McKinnel May 12 '15 at 22:22
  • @RoryMcKinnel still not working. The cells are even smaller :( – Rick May 12 '15 at 22:30
  • My guess is your cells have no constraints to define their height. The answer here is worth reading and explains everything you need to know: http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930 – Rory McKinnel May 12 '15 at 22:31

0 Answers0