Facebook のようなフィードを作成しようとしています。ストーリーボードに自動レイアウト制約が設定された標準のテーブルビューを使用しています。
幅はビューポートの 100% で、高さは画像のアスペクト比に従ってください。
サーバーからの非同期画像読み込みに Haneke の hnk_setImageFromURL を使用しています。
orを使用するオプションをここで見ました。これにより、最初のレンダリングが正常に機能しますが、スクロールを開始すると、高さの計算がうまくいかず、セルが空白になったり、indexOutOfBounds が原因でアプリがクラッシュしたりします。tableview.beginUpdates()
endUpdates()
heightForRowAtIndexPath
サーバーから事前に指定されたアスペクト比で高さを手動で計算するのは唯一のオプションですか?
コードの関連部分
ViewController:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 500
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedItem", forIndexPath: indexPath) as! FeedItemTableViewCell
content.hnk_setImageFromURL(NSURL(string: contentImage)!, placeholder: nil, format: nil, failure: nil, success: {image in
cell.content.image = image
self.beginUpdates()
self.endUpdates()
}
)
}
PS Haneke を使用することによって引き起こされる副作用は、UIImageView に初期サイズを指定する必要があることです。現在、サーバーから画像が読み込まれるときに刺激的なアニメーションを引き起こすプレースホルダー画像を使用しています。