【发布时间】: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 设置
-
谢谢。你是对的。我混淆了这些方法。我需要在一个单元格和另一个单元格之间留出空间。