删除两个自定义单元格之间的间距

在mainStoryboard上有两个自定义单元格的tableView。 我想减less两个单元格之间的间距。 我试图find答案,但找不到任何。 我在下面添加了图像和代码。

在这里输入图像说明

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate { @IBOutlet var tblStoryList: UITableView! var array = PLIST.shared.mainArray override func viewDidLoad() { super.viewDidLoad() //spacing between header and cell self.tblStoryList.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0) //delete separator of UITableView tblStoryList.separatorStyle = .none } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.array.count + 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell cell.headerTitle.text = "First Stage" return cell } let cell = tableView.dequeueReusableCell(withIdentifier: "StoryTableviewCell", for: indexPath) as! StoryTableviewCell //making plist file let dict = self.array[indexPath.row - 1] let title = dict["title"] as! String let imageName = dict["image"] as! String let temp = dict["phrases"] as! [String:Any] let arr = temp["array"] as! [[String:Any]] let detail = "progress \(arr.count)/\(arr.count)" //property to plist file cell.imgIcon.image = UIImage.init(named: imageName) cell.lblTitle.text = title cell.lblSubtitle.text = detail cell.selectionStyle = UITableViewCellSelectionStyle.none return cell } 

标题单元格

  import UIKit class HeaderCell: UITableViewCell { @IBOutlet weak var headerTitle: UILabel! override func layoutSubviews() { super.layoutSubviews() headerTitle.layer.cornerRadius = 25 headerTitle.layer.masksToBounds = true } } 

我想你已经在heightForRowAt indexPath方法的单元格中设置了一些静态高度,将其设置为UITableViewAutomaticDimension 。 并estimatedHeightForRowAt indexPath将其设置为静态值以获得更好的性能。

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 40 //expected minimum height of the cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 

而不要忘记,你需要设置“正确的约束”,以通过这种方法获得所需的结果。 需要约束来让表格单元了解其高度。

尝试这个..

 self.tableView.rowHeight = 0; // in viewdidload [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // in viewdidload -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01f; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return <your header height>; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ return [[UIView alloc] initWithFrame:CGRectZero]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ return <your header view>; } 

也有没有表中的seprator。

您需要相应地为每个单元格返回高度。 例如

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { // return height for first row, ie return 40 } // return height for other cells. ie return 90 } 

你可以设置表每个和每个单元格的高度dynamicAutolay表格视图将需要从Autolayout的单元格,因为你不需要写这2种方法

删除这个方法

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

用ViewDidload方法写这2行

  self.tblView?.rowHeight = UITableViewAutomaticDimension self.tblView?.estimatedRowHeight = 60 

并设置你的imageview底部空间和你要保留的Autolayout例如10和底部布局的关系大于或等于设置优先级为252而不是1000

您将在我的屏幕快照检查中得到清楚的想法以及如何做到这一点在所有自定义单元格中这样做,您可以设置高度dynamic无需计算每个单元格的高度,而不需要返回

在这里输入图像说明