SoFunction
Updated on 2025-04-12

Summary of FAQs on UITableView usage in iOS

1. How to set headerView and its height

 = myHeaderView
 
let height = (UILayoutFittingCompressedSize).height
var frame = 
 = height
 = frame

2. Remove the split line of the excess cell

 = [[UIView alloc] initWithFrame:CGRectZero];

3. How to set the number of sections and rows

extension MyViewController: UITableViewDataSource {
 
 // section number func numberOfSections(in: UITableView) -> Int {
 }
 
 // row number public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 }
 
 // Under section and row, cell public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 }
 
}

4. iOS 8+ automatically calculates row height and section height

 = 80
 = UITableViewAutomaticDimension

In fact, the height of sectionHeader can also be calculated automatically.

 = 20
 = UITableViewAutomaticDimension

Of course sectionFooter is also OK, I won't repeat it

5. Disable the split line that comes with tableview

 = .none

6. Set sectionHeader and sectionFooter, as well as their height

view

extension MyViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
 
 }
 
 func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
 
 }
}

high

extension TTEntranceExamReportViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
 }
 
 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
 }
}

7. Click on the cell to have a shadow, and the shadow disappears when raised

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 (at: indexPath, animated: no)
 // other code
}

8. The issue of automatic indentation of UITableViewCell of iPad

if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
 _tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

Swift version:

if IS_IPAD, #available(iOS 9.0, *) {
  = false
}

9. Set the click effect of UITableviewCell pressed

 = [[PureColorView alloc] initWithColor:[UIColor redColor]];

PureColorView is a class that converts colors into solid color Views, which can be searched online.

10. SectionHeader does not suck the top

let tv = UITableView(frame: , style: .grouped)

11. After using .groupted, there is 20px extra space at the bottom of TableView

 = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: ))

12. On the ios 8 system, click cell to push a vc, and pop it back, and some cell heights will be messed up.

Estimated height is required to be implemented forcibly

Portal

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
 return (tableView, heightForRowAt: indexPath)
}

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to all iOS developers. If you have any questions, you can leave a message to communicate. Thank you for your support.