[英]How to build a Table View with specific number of rows?
我一直在閱讀類似的問題,直到現在都沒有解決此錯誤或找不到它。
我有一個TableView,我需要該表具有特定數量的行。
現在,它可以處理來自數組的通知計數。 在此數組中,我保留了來自服務器的通知。 但是我想如果此數組計數大於40,則行為40,而不是數組的計數。
這是我的代碼:
class ClassName: UITableViewController, UITabBarControllerDelegate {
public var notifications: [APINotification] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.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.tableView.reloadData()
})
})
}
override func numberOfSections(in tableView: UITableView) -> Int {
return self.notifications.count
}
// There is just one row in every section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// Set the spacing between sections
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let event = self.notifications[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "controller", for: indexPath) as! Controller
cell.bodyLbl.text = event.description
cell.typelbl.text = event.type
cell.typelbl.sizeToFit()
cell.titleLbl.text = event.title
cell.subjectLbl.text = event.subject?.name
return cell
} }
謝謝你的幫助!
您可以將此方法更改為:
override func numberOfSections(in tableView: UITableView) -> Int {
return self.notifications.count > 40 ? 40 : self.notifications.count
}
這將顯示從通知中獲得的盡可能多的行,但是如果大於40,則將僅顯示40。要使其始終顯示40,只需將return 40代替,但這可能會導致應用崩潰,因為數組包含的項目更少超過40。
ps,您顯示40個部分,沒有行將其更改為行,而是需要讓您的代碼碰巧:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.notifications.count > 40 ? 40 : self.notifications.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let event = self.notifications[indexPath.row]
...
}
有幾種方法可以實現此目的:
簡單而簡潔:
return min(self.notifications.count, 40)
快速方法:
guard self.notifications.count < 40 else { return 40 } return self.notifications.count
另一種更詳細的版本:
return self.notifications.count < 40 ? self.notifications.count
一個更簡單的版本:
if self.notifications.count > 40 { return 40 } else { return self.notifications.count }
使用您認為最適合此目的的版本。
如果我覺得自己像斯威夫特(Swifty)(甚至是一個字),我可能會選擇1或2。
嘗試以下代碼:
// There is just one row in every section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.notifications.count > 40{
return 40
}
return self.notifications.count
}
要限制最大部分數,只需檢查數組大小:
override func numberOfSections(in tableView: UITableView) -> Int {
var count = self.notifications.count
if (count > 40) {
count = 40
}
return count
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.