How to create dynamic tableview cell with dynamic tableview height in iOS

前端 未结 4 780
太阳男子
太阳男子 2021-01-26 23:14

I want to increase tableview cell and tableview height based on content. Suppose tableview contain 2 record and his 1st cell height is 100 and 2nd cell height is 25 then tablevi

相关标签:
4条回答
  • 2021-01-26 23:28

    If you are running iOS 8+, You can use:

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 80 // your desired or expected height
    

    properties.

    1. for this to take effect you should not have any height set in heightForRowAtIndexpath
    2. You should set the cell constraints i.e., constraints for the elements present inside cell, so the set constraints are enough for the tableviewcell to calculate it's height in run time
    0 讨论(0)
  • 2021-01-26 23:29

    You can definitely do that,

    1. First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.

    2. Make sure your delegated are set as below

      -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
            return 44;
      } 
      
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
          return UITableViewAutomaticDimension;
      }
      
    3. Set height constraint of your tableView and make outlet of that constraint.

    4. Add below method to your class where you want to resize your tableView dynamically.

      - (void)adjustHeightOfTableview
      {
      
          CGFloat height = self.tableView.contentSize.height;
         //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
      
         /* 
           Here you have to take care of two things, if there is only    tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
           Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
        */
      
        // now set the height constraint accordingly
        self.constraintHeightTableView.constant = height;
      
       //If you want to increase tableView height with animation you can do that as below.
      
       [UIView animateWithDuration:0.5 animations:^{
             [self.view layoutIfNeeded];
       }];
      }
      
    5. Call this method when you are ready with the dataSource for the table, and call the method as

      dispatch_async(dispatch_get_main_queue(), ^{
      
          [self.tableView reloadData];
      
          //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
          [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
      });
      
    0 讨论(0)
  • 2021-01-26 23:37

    Solution in swift 3

    Step 1. Set the top, bottom, leading and trailing constraints (do not make it constant height, but do set a constant height constraint as of now). Now we gonna change this height dynamically based on the number of cells and its height.

    Step 2. Drag an outlet of that height constraint to the class, and copy this function anywhere in the class.

        func adjustHeightOfTableview() {
        let height: CGFloat = tableview.contentSize.height
        self.outLetOfTheHeightConstraint.constant = height
    
    }
    

    Step 3.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        DispatchQueue.main.async {
                self.tableview.reloadData()
                self.perform(#selector(self.adjustHeightOfTableview))
            }
    }
    
    0 讨论(0)
  • 2021-01-26 23:41
    self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);
    
    0 讨论(0)
提交回复
热议问题