有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
有没有办法增加UITableViewCell之间的间距?
我已经创建了一个表,每个单元格只包含一个图像。图像被这样分配给单元格:
cell.imageView.image = [myImages objectAtIndex:indexPath.row];
但这使得图像放大并适合整个细胞,并且图像之间没有间隔。
或者让我们这样说,图像的高度是50,我想在图像之间增加20的间距。有什么办法可以做到吗?
当前回答
查看我在GitHub上的解决方案,其中包含UITableView的子类,并使用Objective-C的运行时特性。 它基本上使用了苹果的私有数据结构uitableviewwrodata,我得到了搜索UITableView的私有运行时头:
https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UITableView.h,
这里是你想要的私有类,它包含了你需要的所有东西来布局你的单元格的空间,而不需要在单元格的类中设置它:
https://github.com/JaviSoto/iOS10-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UITableViewRowData.h
其他回答
将section中的行数更改为1 您更改了区段数而不是行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
这里是行与行之间的行距
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
我重写这个函数是UITableViewCell的子类,它对我来说工作正常
override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 5, left: 8, bottom: 5, right: 8)
contentView.frame = contentView.frame.inset(by: margins)
contentView.layer.cornerRadius = 8
}
我认为这是最干净的解决方案:
class MyTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
layoutMargins = UIEdgeInsetsMake(8, 0, 8, 0)
}
}
例子在swift 3..
Crease a single view application add tableview in view controller add a customcell for tablview cell view controller code is bellow like class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var arraytable = [[String:Any]]() override func viewDidLoad() { super.viewDidLoad() arraytable = [ ["title":"About Us","detail":"RA-InfoTech Ltd -A Joint Venture IT Company formed by Bank Asia Ltd"], ["title":"Contact","detail":"Bengal Center (4th & 6th Floor), 28, Topkhana Road, Dhaka - 1000, Bangladesh"] ] tableView.delegate = self tableView.dataSource = self //For Auto Resize Table View Cell; tableView.estimatedRowHeight = 44 tableView.rowHeight = UITableViewAutomaticDimension //Detault Background clear tableView.backgroundColor = UIColor.clear } func numberOfSections(in tableView: UITableView) -> Int { return arraytable.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } // Set the spacing between sections func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 10 } // Make the background color show through func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = UIView() headerView.backgroundColor = UIColor.clear return headerView } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! CustomCell cell.tv_title.text = arraytable[indexPath.section]["title"] as! String? cell.tv_details.text = arraytable[indexPath.section]["detail"] as! String? //label height dynamically increase cell.tv_details.numberOfLines = 0 //For bottom border to tv_title; let frame = cell.tv_title.frame let bottomLayer = CALayer() bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width, height: 1) bottomLayer.backgroundColor = UIColor.black.cgColor cell.tv_title.layer.addSublayer(bottomLayer) //borderColor,borderWidth, cornerRadius cell.backgroundColor = UIColor.lightGray cell.layer.borderColor = UIColor.red.cgColor cell.layer.borderWidth = 1 cell.layer.cornerRadius = 8 cell.clipsToBounds = true return cell } } Download full source to Github : link https://github.com/enamul95/CustomSectionTable
这是实际的解决方案
使用节而不是行因为每一行都有一个节所以你可以给页眉留出空间节脚这是代码,把它粘贴到你的视图控制器类中,在这里你创建tableview outlet
如果你想要顶部间距使用heightForHeaderInSection 如果你想要底部间距,只需使用heightForFooterInSection如下所示
复制粘贴即可
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let v = UIView()
v.backgroundColor = UIColor.clear
return v
}
func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}