Summary of common problems in using UITableView in iOS
- 2021-12-04 11:29:38
- OfStack
1. How to set headerView and its height
tableView.tableHeaderView = myHeaderView
let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame
2. Remove the dividing line of redundant cell
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
3. How to set section number and row number
extension MyViewController: UITableViewDataSource {
// section Number
func numberOfSections(in: UITableView) -> Int {
}
// row Number
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
// In section And row Under, cell
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
}
4. iOS 8 + Automatic calculation of row height and section height
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
In fact, the height of sectionHeader can also be calculated automatically
tv.estimatedSectionHeaderHeight = 20
tv.sectionHeaderHeight = UITableViewAutomaticDimension
Of course, sectionFooter is ok, so I won't go into details
5. Disable the dividing line of tableview
tv.separatorStyle = .none
6. Set sectionHeader and sectionFooter and their heights
view
extension MyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
}
}
Height
extension TTEntranceExamReportViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
}
}
7. Click cell to have a shadow. When you lift it, the shadow disappears
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: no)
// other code
}
8. The problem of UITableViewCell automatic indentation of iPad
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Swift version:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
0
9. Set the click effect of UITableviewCell pressing
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
1
PureColorView is a class that converts color into solid color View, which can be found on the Internet
10. sectionHeader does not attract roof
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
2
11. After using. groupted, there is an extra blank space of 20px at the bottom of TableView
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
12. On the ios 8 system, click cell push1 vc, and then pop comes back, and some cell heights will be messed up
Need to force the lower estimated height
Portal
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
4
Summarize