【发布时间】:2015-03-25 18:39:29
【问题描述】:
所以我有一个 UITableView 加载了许多引号列。输入显示此数据的UITableView 后,单元格最初似乎无法正确加载,并且文本看起来确实被压扁和/或截断,大约 1/2 秒后,它可以正确加载并且一切正常。
每次我加载表格视图时都会发生这种情况。
例如:
.estimatedrowheight...or is it because the view cannot load the data into theUITableView` 的速度够快吗?但是我尝试修改行高无济于事。
编辑
当我像这样明确设置高度时 -
captionsTableView.rowHeight = 80
它工作得很好......因为每个单元格以 80 点的高度加载,没有初始加载故障。
但是使用captionsTableView.rowHeight = UITableViewAutomaticDimension
和captionsTableView.estimatedRowHeight = 80.0 打破它
编辑 2 - 代码
class CaptionsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var captionSegment: UISegmentedControl!
@IBOutlet weak var captionText: UINavigationItem!
@IBOutlet weak var captionSearchBar: UISearchBar!
@IBOutlet weak var captionsTitle: UILabel!
var receiveImage:UIImage!
//var receiveCategoryText:String!
//var firstNameCell: UITableViewCell = UITableViewCell()
//var firstNameText: UITextField = UITextField()
var model:ModelData!
let tapRec = UITapGestureRecognizer()
var currentCell: UITableViewCell!
var fav: String!
let basicCellIdentifier = "CustomCells"
@IBOutlet weak var captionsTableView: UITableView!
var c: CustomCells!
var captionJSON: JSON = nil
override func viewDidLoad() {
super.viewDidLoad()
model = (self.tabBarController as CaptionTabBarController).model
captionJSON = model.quoteSelection()
captionText.title = model.categoryName
captionsTableView.delegate = self
captionsTableView.dataSource = self
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CherrySwash-Regular", size: 25)!, NSForegroundColorAttributeName: UIColor(red:0.0/255.0, green: 159.0/255.0, blue: 172.0/255.0, alpha: 1.0)]
configureTableView()
captionsTableView.reloadData()
tapRec.numberOfTapsRequired = 2
tapRec.addTarget(self, action: "tappedView")
self.view.addGestureRecognizer(tapRec)
self.view.userInteractionEnabled = true
}
func configureTableView() {
captionsTableView.rowHeight = UITableViewAutomaticDimension
captionsTableView.estimatedRowHeight = 80.0
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//deselectAllRows()
//captionsTableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
captionsTableView.reloadData()
}
func deselectAllRows() {
if let selectedRows = captionsTableView.indexPathsForSelectedRows() as? [NSIndexPath] {
for indexPath in selectedRows {
captionsTableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return captionJSON.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return customCellAtIndexPath(indexPath)
}
func customCellAtIndexPath(indexPath:NSIndexPath) -> CustomCells {
var cell = captionsTableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as CustomCells
setTitleForCell(cell, indexPath: indexPath)
setSubtitleForCell(cell, indexPath: indexPath)
setImageForCell(cell, indexPath: indexPath)
return cell
}
func setTitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = captionJSON[indexPath.row]["quote"]
cell.mainLabel.text = item.stringValue
}
func setSubtitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = captionJSON[indexPath.row]["author"]
cell.creditLabel.text = item.stringValue
}
func setImageForCell(cell:CustomCells, indexPath:NSIndexPath) {
//let item = Array(Array(model.quoteItems.values)[indexPath.row])[1] as? String
//if (item == "fav") {
//cell.favImgView.image = UIImage(named: "icon-heart-fav")!
//} else {
cell.favImgView.image = UIImage(named: "icon-heart")!
//}
}
func tappedView(){
//need to add filled icon, as well as logic to not change image upon select of favorited cell
var image : UIImage = UIImage(named: "icon-heart-fav")!
c.favImgView.image = image
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
c = captionsTableView.cellForRowAtIndexPath(indexPath) as CustomCells
var captiontabBar:CaptionTabBarController = CaptionTabBarController()
model.quote = c.mainLabel.text!
model.author = c.creditLabel.text!
println(" quote!!! " + model.quote)
self.tabBarController?.selectedIndex = 0
//let secondViewController:SelectionsController = SelectionsController()
//self.presentViewController(secondViewController, animated: true, completion: nil)
//let returnController = self.storyboard!.instantiateViewControllerWithIdentifier("selections") as? UIViewController
//self.presentViewController(returnController!, animated: true, completion: nil)
/*for tempCell: UITableViewCell in tableView.visibleCells() as [UITableViewCell] {
var image : UIImage = UIImage(named: "icon-heart")!
tempCell.imageView?.image = image
}
var image : UIImage = UIImage(named: "icon-heart-overview")!
c.favImgView?.image = image*/
}
}
class CustomCells: UITableViewCell {
@IBOutlet var mainLabel: UILabel!
@IBOutlet var creditLabel: UILabel!
@IBOutlet var favImgView: UIImageView!
}
【问题讨论】:
-
如果将estimatedRowHeight 修改为一个常数值会怎样?虽然我不认为这是你的问题。看动画,
viewDidAppear中发生了什么或者信息是异步加载的。 -
如果没有别的,你应该得到 +1 只是为了添加动画......发布你的表格代码。
-
没有看到更多代码,我只能猜测你实现了什么,但也许这个相关问题会有所帮助:stackoverflow.com/questions/19374699/…
-
@SamB 哦,是的,哈哈,当然……呃……发帖
-
我试图重现您的错误,但我不确定我的数据设置是否与您的相同。对我来说,如果我删除了 viewDidAppear 方法,它就可以正常工作(我还可以消除 viewDidLoad 中对 reloadData 的其他调用,并且它可以正常工作)。
标签: ios xcode uitableview swift