自定义UITableViewCell与笔尖和自动布局

时间:2015-09-30 16:05:46

标签: ios uitableview autolayout

我现在已经厌倦了使用UITableView的自动布局......

Apple的文档和代码演示似乎都适用于他们,但是当我尝试它时,我没有运气。我甚至从头开始删除并重建笔尖,看看我是否无意中设置了属性......两次。

麻烦的是,这一次我有自动布局单元工作,呜?不会。他们只有在滚出视口并重新进入时才会调整大小。(我猜是通过某种方式重新加载)。

有没有人遇到过这个问题?有一些类似于它的问题,但没有修复,只讨论它必须是一个错误。

这是我的代码:

使用UITableView的{​​{1}} IBOutlet方法调用:

didSet

的cellForRowAtIndexPath:

private func loadHeadlinesTableView()
{
    headlinesTableView.delegate = self
    headlinesTableView.dataSource = self

    //  Register classes/nibs

    headlinesTableView.registerClass(HeadlineHeroTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.HeroHeadline)
    headlinesTableView.registerNib(UINib(nibName: "HeadlineHeroTableViewCell", bundle: nil), forCellReuseIdentifier: CellIdentifiers.HeroHeadline)

    headlinesTableView.registerClass(HeadlineBigTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.BigHeadline)
    headlinesTableView.registerNib(UINib(nibName: "HeadlineBigTableViewCell", bundle: nil), forCellReuseIdentifier: CellIdentifiers.BigHeadline)

    headlinesTableView.registerClass(HeadlineMediumTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.MediumHeadline)
    headlinesTableView.registerNib(UINib(nibName: "HeadlineMediumTableViewCell", bundle: nil), forCellReuseIdentifier: CellIdentifiers.MediumHeadline)

    headlinesTableView.registerClass(HeadlineSmallTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.SmallHeadline)
    headlinesTableView.registerNib(UINib(nibName: "HeadlineSmallTableViewCell", bundle: nil), forCellReuseIdentifier: CellIdentifiers.SmallHeadline)

    //  Automatic cell height

    headlinesTableView.rowHeight = UITableViewAutomaticDimension
    headlinesTableView.estimatedRowHeight = 128

    headlinesTableView.backgroundView = nil
    headlinesTableView.backgroundColor = UIColor.clearColor()
}

willDisplayCell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if tableView.isEqual(headlinesTableView)
    {
        //  Headlines

        var cell: HeadlineTableViewCell!

        switch indexPath.section
        {
        case HeadlineSections.Index.HeroHeadlines:

            //  Hero

            cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifiers.HeroHeadline, forIndexPath: indexPath) as? HeadlineHeroTableViewCell

        case HeadlineSections.Index.BigHeadlines:

            //  Big

            cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifiers.BigHeadline, forIndexPath: indexPath) as? HeadlineBigTableViewCell

        case HeadlineSections.Index.MediumHeadlines:

            //  Medium

            cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifiers.MediumHeadline, forIndexPath: indexPath) as? HeadlineMediumTableViewCell

        case HeadlineSections.Index.SmallHeadlines:

            //  Small

            cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifiers.SmallHeadline, forIndexPath: indexPath) as? HeadlineSmallTableViewCell

        default:
            break
        }

        //  Return cell

        return cell
    }

    return UITableViewCell()
}

didEndDisplayingCell:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    if tableView.isEqual(headlinesTableView)
    {
        //  Headlines

        let headlineCell = cell as! HeadlineTableViewCell
        let headline = headlineForIndexPath(indexPath)

        headlineCell.configureWithHeadline(headline)

        if let thumbnailURL = headline.thumbnailURL
        {
            headlineImageForURL(thumbnailURL, completionHandler: { (thumbnail) -> () in

                headlineCell.configureWithThumbnail(thumbnail)
            })
        }
    }
}

自定义单元格类:

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    if tableView.isEqual(headlinesTableView)
    {
        //  Headlines

        let headline = headlineForIndexPath(indexPath)

        if let cacheKey = headline.thumbnailURL?.absoluteString, let downloader = headlineImagesDownloading[cacheKey]
        {
            downloader.cancelRetreivingThumbnailImage()

            headlineImagesDownloading.removeValueForKey(cacheKey)
        }
    }
}

这是我自定义单元格笔尖的一些截图。它更先进,但现在我只是在争取一个标签......

enter image description here

enter image description here

enter image description here

我知道这很多但是我真的不知道我在哪里可能会出错...谢谢

1 个答案:

答案 0 :(得分:2)

好吧,经过大量的工作并且几乎不得不在“意外”将我的Mac放在墙上之前购买AppleCare,似乎有任何调用tableView(_:willDisplayCell:forRowAtIndexPath:),你可能会影响一个单元格的内容,即喜欢我的单元格配置方法会中断自动布局。

我确定我读到的地方tableView(_:willDisplayCell:forRowAtIndexPath:)应该用于单元格配置,但仔细看看Apple Docs说:

  

表视图在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示之前自定义单元格对象。此方法为委托提供了覆盖表视图前面设置的基于状态的属性的机会,例如选择和背景颜色。委托返回后,表视图仅设置alpha和frame属性,然后仅在行滑入或滑出时为其设置动画。

“在显示之前自定义单元格对象”,而不是其内容。

令人恼火的是,有一点混乱,因为tableView(_:cellForRowAtIndexPath:)没有说你应该自定义该方法中的内容:

  

返回的UITableViewCell对象通常是应用程序因性能原因而重用的对象。您应该通过向tableView发送dequeueReusableCellWithIdentifier:消息来获取先前创建的标记为重用的单元对象。根据单元格是否为分隔符以及数据源提供的信息(例如附件视图和编辑控件)自动设置表格单元格的各种属性。

用邓布利多的话说,“这很有趣。”

相关问题