我有一個不可滾動的TableView。我希望它的高度由條目的數量決定。 為了做到這一點,我在代碼中使用了TableView的高度約束(=CGFloat(data.count) * tableView.rowheight)。這是好的做法嗎?
我注意到當我洗掉TableView的最后一行時,它或多或少地跳過了影片。這是否是因為表格的高度調整速度比影片需要的速度快? 我怎樣才能解決這個問題?
var data = [1, 2, 3, 4, 5,6,7,8,9, >10]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 50.
tableViewHeight.constant = CGFloat(data.count) * tableView.rowHeight
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView。UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "(data[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
data.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableViewHeight.constant = CGFloat(self. data.count) * self.tableView.rowHeight
}
}
override func setEditing(_ editing。Bool, animated: Bool) {
super.setEditing(! isEditing, animated: true)
tableView.setEditing(! tableView.isEditing, animated: true)
}
}
uj5u.com熱心網友回復:
為了避免你所遇到的問題,我注意的是,
我注意的是,你所遇到的問題。
- 盡量避免任何常數或尺寸值。
- 使用
constraints并嘗試在一個地方(StoryBoard、Xib或代碼中)定義它。
因為它的高度是由它的內容決定的,附加到螢屏邊緣是行不通的。
至于你的疑問,
- 嘗試檢查故事板和代碼中是否出現任何沖突的約束。例如,如果整個
tableview在故事板中被定義,為什么tableView.rowHeight = 50也不在那里。 - 在洗掉時,
tableView.deleteRows()有.autometic影片。根據檔案,case automatic = 100 //在iOS 5.0中可用。為你選擇一個合適的影片風格。讓我們把它設定為.none. 。
請檢查下面的審查片段是否有效。
var data = [1, 2,3。 4,5,6, 7, 8,9,10]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 50.
tableViewHeight.constant = CGFloat(data.count) * tableView.rowHeight
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.tableViewHeight.constant = CGFloat(self. data.count) * self.tableView.rowHeight.
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "(data[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
// I think this is causing the problem.
if editingStyle == .delete {
data.remove(at: indexPath.row)
//不使用影片。
tableView.deleteRows(at: [indexPath], with: .none)
///不會在這里更新約束條件。
//self.tableViewHeight.constant = CGFloat(self.data.count) * self.tableView.rowHeight。
}
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(! isEditing, animated: true)
tableView.setEditing(! tableView.isEditing, animated: true)
}
}
如果還沒有的話,也要檢查一下這些,
uj5u.com熱心網友回復:
不,它不是。
通常情況下,表視圖占用整個螢屏,并且應該通過約束條件連接到螢屏邊緣
UITableView是UIScrollView的子類,這意味著如果內容尺寸大于視圖尺寸--它將很容易被滾動如果按照設計,它不應該占用全屏,那么表格視圖框架應該被設定為顯示的許多單元格的最大預期尺寸。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/311738.html
標籤:
