【问题标题】:I need to give space between one cell and another我需要在一个单元格和另一个单元格之间留出空间
【发布时间】:2018-01-17 11:24:04
【问题描述】:

我没有找到我的问题。 我需要在一个单元格和另一个单元格之间留出空间。

我写了 self.tableView Storyline.delegate = self 因为我读到这可能是问题所在,但不知道它是否正确。

这是我的代码:

class ClassName: UIViewController, UITableViewDataSource, UITabBarControllerDelegate, UITableViewDelegate {
    public var notifications: [APINotification] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
        tableViewStoryline.rowHeight = UITableViewAutomaticDimension
        tableViewStoryline.estimatedRowHeight = 140
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(_ animated: Bool) {
        self.notifications = []
        self.getLastNotifications()
        APIAuth.shared.count_badge = 0
        self.tabBarController?.tabBar.items![0].badgeValue = nil
    }

    public func getLastNotifications() {
        let req = Notification()
        req.getLastNotifications(onComplete: {events in
            self.notifications = events

            DispatchQueue.main.async(execute: {
                self.tableViewStoryline.delegate = self
                self.tableViewStoryline.dataSource = self
                self.tableViewStoryline.sectionHeaderHeight = 10
                self.tableViewStoryline.reloadData()
            })
        })
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    } 

    // There is just one row in every section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.notifications.count > 4 {
            return 4
        } else {
            return self.notifications.count
        }
    }

    // Set the spacing between sections
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell = tableView.dequeueReusableCell(withIdentifier: "controller")! as UITableViewCell
        titleLbl.text = notifications[indexPath.row].title
        bodyLbl.text =  notifications[indexPath.row].description
        typeLbl.text = notifications[indexPath.row].type

        return cell!
    } 
}

有人知道问题出在哪里吗? 是否缺少某些代码?

谢谢!

【问题讨论】:

  • 现在您已经为单元格配置了自动高度(将由您的自动布局规则定义)。如果你需要一个固定的高度,你可以通过rowHeight 或通过实现func tableView(UITableView, heightForRowAt: IndexPath) 来提供它。如果您的问题是关于这些部分的,您可以放置​​一个断点并查看是否调用了该方法并确保您也提供了标题视图(viewForHeaderInSection ...)。我希望这是有道理的......
  • 您在代码中分配给标题视图的位置我没有看到委托 viewForHeaderInSectionimplemnted 或 tableview.headerview 设置
  • 谢谢。你是对的。我混淆了这些方法。我需要在一个单元格和另一个单元格之间留出空间。

标签: ios xcode swift3


【解决方案1】:

我想这就是你想要的:

    func numberOfSections(in tableView: UITableView) -> Int {
        if self.notifications.count > 4 {
            return 4
        } else {
            return self.notifications.count
        }
    }

    // There is just one row in every section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

那么您还必须更改 cellForRowAt 以考虑到这一点。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        cell = tableView.dequeueReusableCell(withIdentifier: "controller")! as UITableViewCell
        titleLbl.text = notifications[indexPath.section].title
        bodyLbl.text =  notifications[indexPath.section].description
        typeLbl.text = notifications[indexPath.section].type

        return cell!
    }

您希望notifications.count 部分和每个部分都有一行,而不是一个部分包含notifications.count 行。现在将部分标题的高度设置为 10 点将使单元格之间出现空格。

您可以考虑其他一些选项,请参阅我的other answer。

【讨论】:

  • 这太棒了!正是我需要的。非常感谢!!
  • @BelenDominguezGarcia 很高兴为您提供帮助
【解决方案2】:

您使用了错误的委托函数。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

用于标题的高度而不是单元格的高度。

使用func tableView(UITableView, heightForRowAt: IndexPath) 你会得到正确大小的单元格。

希望这会有所帮助!

【讨论】:

猜你喜欢
  • 2021-02-02
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2016-05-09
  • 1970-01-01
相关资源
最近更新 更多