Hide cells in a UITableView with static cells - and no autolayout crash

前端 未结 9 1713
庸人自扰
庸人自扰 2021-01-01 16:01

I have a table view form created using Static Cells in IB/Storyboard. However, I need to hide some of the cells at runtime depending on certain conditions.

I have fo

相关标签:
9条回答
  • 2021-01-01 16:36

    Hide the cells on the storyboard and set the height to 0:

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let cell: UITableViewCell = super.tableView(tableView, cellForRowAtIndexPath:indexPath)
        return cell.hidden ? 0 : super.tableView(tableView, heightForRowAtIndexPath:indexPath)
        }
    }
    
    0 讨论(0)
  • 2021-01-01 16:46

    If you ensure there's no constraint touching the bottom edge of the cell, autolayout shouldn't barf (tested on iOS 6.0, 6.1, 7.0). You'll still 'anchor' to the top edge and have to pin the heights. (You can do the reverse and anchor to the bottom, of course.)

    If your layout depends on both the top and bottom edge positions, it may be possible to programmatically remove the constraints (they're just objects, after all).

    0 讨论(0)
  • 2021-01-01 16:46

    The best way for me was to modify numberOfRowsInSection method. I removed datasource which i did not want to display. Best solution for me, because everything is in one function.

    (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section==0) {
        NSUInteger productCount = _products.count;
        for(loop trough your data) {
          if(your condition is true)
             productCount--;
          }
        }
        return productCount;
    } else
        return self.groups.count;
    }
    
    0 讨论(0)
提交回复
热议问题