UITableView和自定义单元格之间的替代边距

时间:2019-07-04 07:14:39

标签: swift uitableview tvos

我有如下代码创建的UITableView:

 lazy var tableView:UITableView = {
    let tv = UITableView()
    tv.delegate = self
    tv.dataSource = self
    tv.remembersLastFocusedIndexPath = false
    tv.translatesAutoresizingMaskIntoConstraints = false
    return tv
}

我将其限制为父视图:

private func setup() {
    tableView.register(LiveTVCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(tableView)
    tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 20).isActive = true
    tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
    tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
    tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
}

这里的cusom细胞只有红色背景。如图所示,该单元格显示了从屏幕左侧开始的90个点,表格视图开始了。我尝试使用layoutMarginscontentInset无效。如果我做类似的事情:print(self.tableView.layoutMargins),则表明左边距确实为90。

我能够克服此问题的唯一方法是添加以下内容:tv.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: -180, bottom: 0, trailing: -90)我讨厌此解决方案,因为它依赖于幻数。

净额,如果没有上述漏洞修复程序,如何删除这90点的左边距?

已请求更多代码,因此我在下面包括了tableview委托和数据源功能:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.liveArray.first?.shows.count ?? 0
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LiveTVCell

    if !self.fetchedRows.contains(indexPath.row) {
        self.fetchedRows.append(indexPath.row)
        self.spinner.start()
        MediaFeedController.retrieveShows(showIDs: liveArray.first!.shows,forSection:indexPath.row) { [unowned self](row) in
            self.spinner.stop()
            guard row != nil else {return}
            UIView.performWithoutAnimation {
                self.tableView.reloadRows(at: [IndexPath(row: row!, section: 0)], with: .none)
            }
        }
    }

    cell.showID = liveArray.first!.shows[indexPath.row]
    //print(self.tableView.layoutMargins)

    return cell
}

func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
    return false
}

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

这是tableviewcell。请记住,如果我从tableviewcell中删除所有这些并仅设置背景颜色,则会出现此问题:

import UIKit

class LiveTVCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var showID:Int! {
    didSet {
        setup()
    }
}

private var collectionView: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

override func layoutSubviews() {
    super.layoutSubviews()
}

func setup() {


    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal

    collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor(named: "collectionView")
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.remembersLastFocusedIndexPath = true
    collectionView.register(ChannelCVCell.self, forCellWithReuseIdentifier: "channelCell")
    collectionView.register(ProgramCVCell.self, forCellWithReuseIdentifier: "programCell")

    self.addSubview(collectionView)
    collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    collectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}

// MARK: - Collectionview Delegate & Datasource

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.section == 0 {
        let channelCell = collectionView.dequeueReusableCell(withReuseIdentifier: "channelCell", for: indexPath) as! ChannelCVCell
        channelCell.showID = showID
        return channelCell
    } else {
        let programCell = collectionView.dequeueReusableCell(withReuseIdentifier: "programCell", for: indexPath) as! ProgramCVCell
        programCell.showID = showID
        return programCell
    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.section == 0 {
        return CGSize(width: 177, height: 70)
    } else {
        return CGSize(width: collectionView.frame.size.width, height: 70)
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    if section == 0 {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    } else {
        return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
    }
}

// MARK: - CollectionView Focus Delegates

func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    //guard let index = context.nextFocusedIndexPath?.row else {return}
}

override func prepareForReuse() {
    self.collectionView.removeFromSuperview()
}

}

更新:

您可以完全删除自定义单元格,cellForRowAt中没有任何内容,并返回通用的UITableViewCell,并且该表的左侧布局边距为90。实际上,第二次将表添加到子视图中并将其锚定为90。

0 个答案:

没有答案