xcode 8 swift 3 UItable单元格之间的空间?
我试图在UItableview
单元格之间UItableview
出空间我已经检查了谷歌我发现的所有post都超过3年,当我尝试应用它们时我得到了很多错误,是否有可能在UItableview
单元格之间UItableview
出空间?
我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell cell.model = self.modelAtIndexPath(indexPath) let url = URL(string: cell.model.imageName) cell.imgBack.kf.setImage(with: url) return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //send back playlist count if (section == 0) { return self.lists_arr.count } return 0 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated:true) } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { let imageCell = cell as! ImageCell self.setCellImageOffset(imageCell, indexPath: indexPath) } //number of sections in table func numberOfSections(in tableView: UITableView) -> Int { return 1 } func modelAtIndexPath(_ indexPath: IndexPath) -> CellPlaylist { return self.lists_arr[(indexPath as NSIndexPath).row % self.lists_arr.count] } func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView == self.tblMain) { for indexPath in self.tblMain.indexPathsForVisibleRows! { if let imgCel : ImageCell = self.tblMain.cellForRow(at: indexPath) as? ImageCell { self.setCellImageOffset(imgCel, indexPath: indexPath) } } } } func setCellImageOffset(_ cell: ImageCell, indexPath: IndexPath) { let cellFrame = self.tblMain.rectForRow(at: indexPath) let cellFrameInTable = self.tblMain.convert(cellFrame, to:self.tblMain.superview) let cellOffset = cellFrameInTable.origin.y + cellFrameInTable.size.height let tableHeight = self.tblMain.bounds.size.height + cellFrameInTable.size.height let cellOffsetFactor = cellOffset / tableHeight cell.setBackgroundOffset(cellOffsetFactor) }
TableCell的类:
class ImageCell: UITableViewCell { @IBOutlet weak var lblTitle: UILabel! @IBOutlet weak var imgBack: UIImageView! @IBOutlet weak var imgBackTopConstraint: NSLayoutConstraint! @IBOutlet weak var imgBackBottomConstraint: NSLayoutConstraint! let imageParallaxFactor: CGFloat = 70 var imgBackTopInitial: CGFloat! var imgBackBottomInitial: CGFloat! var model: CellPlaylist! { didSet { self.updateView() } } override func awakeFromNib() { self.clipsToBounds = true self.imgBackBottomConstraint.constant -= 2 * imageParallaxFactor self.imgBackTopInitial = self.imgBackTopConstraint.constant self.imgBackBottomInitial = self.imgBackBottomConstraint.constant } func updateView() { // self.imgBack.imageFromServerURL(urlString: self.model.imageName) //self.getImage(url: self.model.imageName,imgView: self.imgBack) //self.model.imageName self.layer.cornerRadius = 10 self.layer.masksToBounds = true self.layer.cornerRadius = 8 self.layer.shadowOffset = CGSize(width: 0, height: 3) self.layer.shadowRadius = 3 self.layer.shadowOpacity = 0.3 self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 8, height: 8)).cgPath self.layer.shouldRasterize = true self.layer.rasterizationScale = UIScreen.main.scale self.contentView.layoutMargins.top = 20 self.lblTitle.text = self.model.title } func setBackgroundOffset(_ offset:CGFloat) { let boundOffset = max(0, min(1, offset)) let pixelOffset = (1-boundOffset)*2*imageParallaxFactor self.imgBackTopConstraint.constant = self.imgBackTopInitial - pixelOffset self.imgBackBottomConstraint.constant = self.imgBackBottomInitial + pixelOffset } }
故事板:
我得到了什么
:
我想要的是 :
将Tablview BG颜色设置为白色,并在要查看的单元格中设置BG视图颜色我已设置bgview BG颜色较浅并使BGview高度较小然后单元格我的意思是如果你想设置空间10像素保持Bg高度较小然后单元格10像素
这是我的手机 这是出局
您可以让每个单元格在组tableView中获取一个部分,然后设置该部分的页脚。
试试我的代码:
import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView.register(UINib.init(nibName: "VC1Cell", bundle: nil), forCellReuseIdentifier: "VC1Cell") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: -- tableview delegate func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let view:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: 10)) view.backgroundColor = .clear return view } func numberOfSections(in tableView: UITableView) -> Int { return 10 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 44 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell:VC1Cell = tableView.dequeueReusableCell(withIdentifier: "VC1Cell") as! VC1Cell return cell } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 10.0 } }
结果:
斯威夫特4回答:
在您的自定义单元格类中添加此function
override func layoutSubviews() { //set the values for top,left,bottom,right margins let margins = UIEdgeInsetsMake(0, 0, 10, 0) contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins) }
您可以根据需要更改值
- 我怎样才能将阴影添加到一个圆圈UIImageView或UIView?
- 卸载应用程序不会删除应用程序组数据。我是否必须手动删除应用程序组容器和内容?
- .xcconfig? 如何设置环境variables
- iOS中的随机颜色
- 将外部附件连接到3.5毫米耳机插孔时无法获得通知
- AWS Mobilehub:Swift 3更新,在启动选项中显示错误
- Swift:一个collectionviewcell中的Actionbutton
- 我如何支持更高的iPhone 5屏幕尺寸?
- didRegisterForRemoteNotificationsWithDeviceToken不在ios8中调用,但didRegister …设置是