UITableViewCells 初始加载视图/显示问题 [英] UITableViewCells initial load view/display issue

查看:29
本文介绍了UITableViewCells 初始加载视图/显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 UITableView 加载多列引号.输入显示此数据的 UITableView 后,单元格似乎最初没有正确加载,文本看起来真的被压扁和/或被切断,大约 1/2 秒后,它正确加载和所有内容工作正常.

So I've got a UITableView loading up many columns of quotes. Upon entering the UITableView that displays this data, the cells seem to initially not be loading properly and the text looks really squished and/or cut off, after about a 1/2 second, it loads properly and everything works fine.

每次加载表格视图时都会发生这种情况.

This occurs every time I load up the table view.

例如:

这是 .estimatedrowheight 的问题吗...还是因为视图无法足够快地将数据加载到UITableView`?但是,我尝试修改行高无济于事.

Is this a problem with the .estimatedrowheight...or is it because the view cannot load the data into theUITableView` fast enough? I've tried modifying the row height to no avail however.

编辑

当我像这样明确设置高度时 -

It looks like when I explicity set the height like this -

captionsTableView.rowHeight = 80

它工作得很好......就像在每个单元格加载 80 点高度一样,没有初始加载故障.

it works just fine...as in the cells load at 80points in height each, without the initial loading glitch.

但是使用 captionsTableView.rowHeight = UITableViewAutomaticDimensioncaptionsTableView.estimatedRowHeight = 80.0 打破它

But using captionsTableView.rowHeight = UITableViewAutomaticDimension and captionsTableView.estimatedRowHeight = 80.0 breaks it

编辑 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!
}

推荐答案

在 (void)viewWillAppear 中添加你的代码:

Add your code in (void)viewWillAppear:

看起来您是在 viewDidload 中进行的.

Looks like you are doing it in viewDidload.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (![self.p_tableView respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)])
    {
        self.p_tableView.rowHeight = UITableViewAutomaticDimension;
        self.p_tableView.estimatedRowHeight = 100.0f;
    }
}

这篇关于UITableViewCells 初始加载视图/显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆