删除两个自定义单元格之间的间距 [英] Remove Spacing between two custom cells
问题描述
我在 mainStoryboard 上有一个带有两个自定义单元格的 tableView.我想减少两个单元格之间的间距.我试图找到答案,但找不到任何答案.我在下面添加了图像和代码.
I have a tableView on mainStoryboard with two custom cells. I would like to reduce the spacing between two cells. I was trying to find the answer but could not find any. I have image and code added below.
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
将其设置为静态值以获得更好的性能.
I think you have set some static height to the cells in heightForRowAt indexPath
method, set it to UITableViewAutomaticDimension
. and estimatedHeightForRowAt indexPath
set it to static value for better performance.
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
}
不要忘记您需要设置正确的约束"才能通过此方法获得所需的结果.需要约束才能让表格单元格知道其高度.
And not to forget that you need to set "correct constraints" to get the desired result by this method. Constraints are required to let know the table cell about its height.
这篇关于删除两个自定义单元格之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!