gpt4 book ai didi

ios - 带有 Nib 和自动布局的自定义 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 10:24:59 26 4
gpt4 key购买 nike

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

Apple 的文档和代码演示似乎都适用于他们,但当我尝试时却没有成功。我什至从头开始删除并重建 Nib ,只是为了看看我是否无意中设置了属性……两次。

问题是,这一次我让自动布局单元格正常工作,哇?不会。它们只会在滚动出视口(viewport)并返回时调整大小。(我猜是通过某种重新加载)。

有人遇到过这个问题吗? SO 上有几个类似的问题,但没有修复,只是讨论它为什么一定是一个错误。

这是我的代码:

调用 UITableViewIBOutlet didSet 方法:

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()
}

cellForRowAtIndexPath:

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()
}

将显示单元格:

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)
})
}
}
}

didEndDisplayingCell:

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)
}
}
}

自定义单元格类:

class HeadlineTableViewCell: UITableViewCell
{
// MARK: - Properties

// MARK: Outlets

@IBOutlet private weak var headlineImageView: UIImageView!

@IBOutlet private weak var headlineTitleLabel: UILabel!
@IBOutlet private weak var headlineDescriptionLabel: UILabel!

// MARK: - View Lifecycle

override func awakeFromNib()
{
super.awakeFromNib()

// Initialization code
}

// MARK: - Methods

func configureWithHeadline(headline: Headline?)
{
headlineTitleLabel?.text = headline?.title
headlineDescriptionLabel?.text = headline?.paragraph
}

func configureWithThumbnail(thumbnail: UIImage?)
{
headlineImageView?.image = thumbnail
}
}

这是我的自定义单元格 Nib 的一些屏幕截图。它更高级,但现在我正在为标签而苦苦挣扎......

enter image description here

enter image description here

enter image description here

我知道很多,但我真的不知道我哪里会出错......谢谢

最佳答案

好吧,经过大量的工作,在“不小心”把我的 Mac 摔到墙上之前几乎不得不购买 AppleCare,似乎任何调用 tableView(_:willDisplayCell:forRowAtIndexPath:) 从而您可能会影响单元格的内容,即像我的单元格配置方法一样,会破坏自动布局。

我确定我在某处读到 tableView(_:willDisplayCell:forRowAtIndexPath:) 应该用于单元格配置,但仔细查看 Apple Docs 会说:

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

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

令人恼火的是,由于 tableView(_:cellForRowAtIndexPath:) 没有说您应该在该方法中自定义内容,所以有点困惑:

The returned UITableViewCell object is frequently one that the application reuses for performance reasons. You should fetch a previously created cell object that is marked for reuse by sending a dequeueReusableCellWithIdentifier: message to tableView. Various attributes of a table cell are set automatically based on whether the cell is a separator and on information the data source provides, such as for accessory views and editing controls.

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

关于ios - 带有 Nib 和自动布局的自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32870810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com