【发布时间】:2015-03-07 03:30:39
【问题描述】:
我有一个 UITableView,当我向上滚动时会更新,但当我向下滚动时它不会更新。此外,当它确实更新时,它有时似乎会“跳过”一个单元格并更新下一个单元格。
- 应填充总共 6 个单元格
- 我在情节提要中创建了 UITableView,为情节提要中的 hashLabel 和 creditLabel 设置了约束
这是初始 TableView 的图像:
并且在向上滚动时,正确更新时:
...当向上滚动“错过”一个单元格时:
当然还有班级:
class HashtagController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var model:ModelData!
var currentCell: UITableViewCell!
@IBOutlet var hashtagTableView: UITableView!
let basicCellIdentifier = "CustomCells"
override func viewDidLoad() {
super.viewDidLoad()
model = (self.tabBarController as CaptionTabBarController).model
hashtagTableView.delegate = self
hashtagTableView.dataSource = self
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CherrySwash-Regular", size: 25)!, NSForegroundColorAttributeName: UIColor(red:27.0/255, green: 145.0/255, blue: 114.0/255, alpha: 1.0)]
configureTableView()
hashtagTableView.reloadData()
}
func configureTableView() {
hashtagTableView.rowHeight = UITableViewAutomaticDimension
hashtagTableView.estimatedRowHeight = 160.0
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//deselectAllRows()
hashtagTableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
hashtagTableView.reloadData()
}
func deselectAllRows() {
if let selectedRows = hashtagTableView.indexPathsForSelectedRows() as? [NSIndexPath] {
for indexPath in selectedRows {
hashtagTableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.quoteItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return customCellAtIndexPath(indexPath)
}
func customCellAtIndexPath(indexPath:NSIndexPath) -> CustomCells {
var cell = hashtagTableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as CustomCells
setTitleForCell(cell, indexPath: indexPath)
setSubtitleForCell(cell, indexPath: indexPath)
return cell
}
func setTitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = Array(Array(model.quoteItems.values)[indexPath.row])[0] as? String
cell.hashLabel.text = item
}
func setSubtitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = Array(model.quoteItems.keys)[indexPath.row]
cell.creditLabel.text = item
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
/*currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
var currentLabel = currentCell.textLabel?.text
var currentAuthor = currentCell.detailTextLabel?.text
model.quote = currentLabel!
model.author = currentAuthor!*/
}
}
class CustomCells: UITableViewCell {
@IBOutlet var hashLabel: UILabel!
@IBOutlet var creditLabel: UILabel!
}
【问题讨论】:
-
ModelData.quoteItems是什么?是NSDictionary吗? -
我还想知道
-setTitleForCell和-setSubTitleForCell中的item是否是空行中的nil。如果有,为什么? -
我已经完成了一个 println 并且所有数据似乎都被打印出来了,没有出现任何错误。
-
您应该将您的字典转换为
Quote对象的数组一次(或至少将所有键放入数组中一次),然后直接从该数组访问数据。 -
我同意 Paul 的观点,如果您关心物品的顺序,
NSDictionary不是您想要的。这也可能是您的问题的根源。
标签: ios uitableview swift ios8