gpt4 book ai didi

ios - 首次加载时 UITableViewCell 变窄

转载 作者:可可西里 更新时间:2023-11-01 02:19:23 24 4
gpt4 key购买 nike

narrowed cells

当单元格第一次出现时,它们看起来像我的图片所示。如果向上滚动然后向下滚动让它们再次显示,它们将是正常的。顶部三个单元格始终正常,而底部变窄。这是我的代码`

import UIKit

class AllCommentsViewController: UIViewController {

@IBAction func unwindToAllComments(sender: UIStoryboardSegue){
if sender.identifier == "unwindToAllComments"{
}
}

override func awakeFromNib() {
let photo = ZLLPhoto(withoutDataWithObjectId: "55cd717700b0875fb6cc125f")
photo.fetch()
self.photo = photo
}

var photo: ZLLPhoto!

var comments = [ZLLComment]()

@IBAction func commentButtonClicked(sender: UIButton){
performSegueWithIdentifier("comment", sender: nil)
}

private func initialCommentsQuery() -> AVQuery {
let query = ZLLComment.query()
query.whereKey("photo", equalTo: photo)
query.limit = 20
query.orderByDescending("likeCount")
query.addDescendingOrder("createdAt")
query.includeKey("score")
query.includeKey("author")
return query
}

func getFirst(){
initialCommentsQuery().findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if objects.count != 0 {
let comments = objects as! [ZLLComment]
self.comments = comments

var indexPaths = [NSIndexPath]()
for i in 0..<objects.count{
var indexPath = NSIndexPath(forRow: i, inSection: 2)
indexPaths.append(indexPath)
}

self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
self.tableView.endUpdates()
self.tableView.footer.endRefreshing()
self.tableView.footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: "loadMore")
self.tableView.header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "refresh")
}
}else{
print(error)
}
self.tableView.footer.endRefreshing()
}
}

func loadMore(){
let last = comments.last!
let query = initialCommentsQuery()

query.whereKey("likeCount", lessThanOrEqualTo: last.likeCount)
query.whereKey("createdAt", lessThan: last.createdAt)

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if objects.count != 0{
var indexPaths = [NSIndexPath]()
for i in 0..<objects.count{
let indexPath = NSIndexPath(forRow: self.comments.count + i, inSection: 2)
indexPaths.append(indexPath)
}

let comments = objects as! [ZLLComment]
self.comments.extend(comments)

self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .None)
self.tableView.endUpdates()
}
}else{
print(error)
}
self.tableView.footer.endRefreshing()
}
}

func refresh(){
let first = comments.first!
let query = initialCommentsQuery()
query.whereKey("likeCount", greaterThanOrEqualTo: first.likeCount)
query.whereKey("createdAt", greaterThan: first.createdAt)

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if objects.count != 0 {
var comments = objects as! [ZLLComment]
comments.extend(self.comments)
self.comments = comments

var indexPaths = [NSIndexPath]()
for i in 0..<objects.count{
var indexPath = NSIndexPath(forRow: i, inSection: 2)
indexPaths.append(indexPath)
}

self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(indexPaths,
withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()

}
}else{
print(error)
}
self.tableView.header.endRefreshing()
}
}

@IBOutlet weak var commentButton: UIButton!

@IBOutlet weak var photoTitle: UILabel!{
didSet{
photoTitle.text = photo.title
}
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "comment" {
let VC = segue.destinationViewController as! CommentPhotoViewController
VC.photo = photo
}else if segue.identifier == "showUser"{
let VC = segue.destinationViewController as! UserInfoTableViewController
VC.user = sender as! ZLLUser
}
}


@IBAction func likeButtonClicked(sender: UIButton) {
let indexPath = tableView.indexPathForCellContainingView(sender)!
let comment = comments[indexPath.row]
let I = ZLLUser.currentUser()
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CommentCell

if !cell.liked{
I.likeComment(comment, completion: { (error) -> Void in
if error == nil {
comment.incrementKey("likeCount")
cell.likeCount += 1
cell.liked = true
}else{
print(error)
}
})
}else{
I.unlikeComment(comment, completion: { (error) -> Void in
if error == nil{
comment.incrementKey("likeCount", byAmount: -1)
cell.likeCount -= 1
cell.liked = false
}else{
print(error)
}
})
}
}
//XIBload
@IBOutlet weak var tableView: UITableView!

@IBOutlet var titleScoreLabel: UILabel?

@IBOutlet var titleLabel: UILabel?

@IBOutlet var photoCell: UITableViewCell!

override func viewDidLoad() {

NSBundle.mainBundle().loadNibNamed("PhotoCell", owner: self, options: nil)

tableView.footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: "getFirst")

let query = ZLLScore.query()
query.whereKey("photo", equalTo: photo)
query.whereKey("scorer", equalTo: ZLLUser.currentUser())
query.countObjectsInBackgroundWithBlock { (count, error) -> Void in
if error == nil {
if count > 0{
self.commentButton.setImage(UIImage(named: "comments_add_comment")!, forState: UIControlState.Normal)
}
}else{
print(error)
}
}
}

}

extension AllCommentsViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("photoCell") as! PhotoCell
cell.loadPhoto2(photo)
return cell
case 1:
tableView
let cell = tableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentCell
cell.loadPhotoInfo(photo)
return cell
default:
let cell = tableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentCell
if indexPath.row == 0{
cell.smallTagImage.image = UIImage(named: "jian")
return cell
}
cell.delegate = self
cell.loadComment(comments[indexPath.row])
return cell
}
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section{
case 2:
return comments.count
default:
return 1
}
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
switch section{
case 0:
return 1
default:
return 10
}
}
}

extension AllCommentsViewController: PhotoCellDelegate{
func photoCellDidTapUserField(photoCell: PhotoCell) {
}

func photoCellDidClickShareButton(photoCell: PhotoCell) {
//
let indexPath = tableView.indexPathForCell(photoCell)!

let share = UIAlertController(title: "分享图片", message: "\(photo.title)", preferredStyle: UIAlertControllerStyle.ActionSheet)
let weibo = UIAlertAction(title: "微博", style: UIAlertActionStyle.Default) { (action) -> Void in

if !WeiboSDK.isWeiboAppInstalled(){
ZLLViewModel.showHintWithTitle("未安装微博应用", on: self.view)
return
}

if !ShareSDK.hasAuthorizedWithType(ShareTypeSinaWeibo){
let authorize = UIAlertController(title: "未获取授权", message: "是否要获取授权", preferredStyle: UIAlertControllerStyle.Alert)
let confirm = UIAlertAction(title: "确认", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
ShareSDK.getUserInfoWithType(ShareTypeSinaWeibo, authOptions: nil, result: { (result, userInfo, errorInfo) -> Void in
if !result {
ZLLViewModel.showHintWithTitle("授权失败!", on: self.view)
return
}

let image = ShareSDK.imageWithData(self.photo.imageFile.getData(), fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: image, title: "c", url: "", description: "d", mediaType: SSPublishContentMediaTypeImage)
ShareSDK.clientShareContent(content, type: ShareTypeSinaWeibo, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
})
})
let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
authorize.addAction(confirm)
authorize.addAction(cancel)

self.presentViewController(authorize, animated: true, completion: nil)
return
}

let image = ShareSDK.imageWithData(self.photo.imageFile.getData(), fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: image, title: "c", url: "", description: "d", mediaType: SSPublishContentMediaTypeImage)
ShareSDK.clientShareContent(content, type: ShareTypeSinaWeibo, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
}

let qZone = UIAlertAction(title: "qq空间", style: UIAlertActionStyle.Default) { (action) -> Void in
//

if !QQApiInterface.isQQInstalled(){
ZLLViewModel.showHintWithTitle("未安装腾讯QQ", on: self.view)
return
}

if !ShareSDK.hasAuthorizedWithType(ShareTypeQQSpace){
let authorize = UIAlertController(title: "未获取授权", message: "是否要获取授权", preferredStyle: UIAlertControllerStyle.Alert)
let confirm = UIAlertAction(title: "确认", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
ShareSDK.getUserInfoWithType(ShareTypeQQSpace, authOptions: nil, result: { (result, userInfo, errorInfo) -> Void in
if !result {
ZLLViewModel.showHintWithTitle("授权失败!", on: self.view)
return
}

let image = ShareSDK.imageWithData(self.photo.imageFile.getData(), fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: image, title: "c", url: "", description: "d", mediaType: SSPublishContentMediaTypeImage)
ShareSDK.clientShareContent(content, type: ShareTypeQQSpace, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
})
})
let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
authorize.addAction(confirm)
authorize.addAction(cancel)

self.presentViewController(authorize, animated: true, completion: nil)
return
}
let image = UIImage(data: self.photo.imageFile.getData())
let data = UIImageJPEGRepresentation(image, 0.1)

let attachment = ShareSDK.imageWithData(data, fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: attachment, title: "c", url: "www.baidu.com", description: "d", mediaType: SSPublishContentMediaTypeImage)
ShareSDK.clientShareContent(content, type: ShareTypeQQSpace, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
}

let weixin = UIAlertAction(title: "微信好友", style: UIAlertActionStyle.Default) { (action) -> Void in
if !QQApiInterface.isQQInstalled(){
ZLLViewModel.showHintWithTitle("未安装腾讯QQ", on: self.view)
return
}

if !ShareSDK.hasAuthorizedWithType(ShareTypeQQSpace){
let authorize = UIAlertController(title: "未获取授权", message: "是否要获取授权", preferredStyle: UIAlertControllerStyle.Alert)
let confirm = UIAlertAction(title: "确认", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
ShareSDK.getUserInfoWithType(ShareTypeQQ, authOptions: nil, result: { (result, userInfo, errorInfo) -> Void in
if !result {
ZLLViewModel.showHintWithTitle("授权失败!", on: self.view)
return
}

let image = ShareSDK.imageWithData(self.photo.imageFile.getData(), fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: image, title: "c", url: "", description: "d", mediaType: SSPublishContentMediaTypeImage)
ShareSDK.clientShareContent(content, type: ShareTypeQQ, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
})
})
let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
authorize.addAction(confirm)
authorize.addAction(cancel)

self.presentViewController(authorize, animated: true, completion: nil)
return
}
let image = ShareSDK.imageWithData(self.photo.imageFile.getData(), fileName: "test1", mimeType: "")
let content = ShareSDK.content("a", defaultContent: "b", image: image, title: "c", url: "", description: "d", mediaType: SSPublishContentMediaTypeImage)

ShareSDK.clientShareContent(content, type: ShareTypeQQ, statusBarTips: true, result: { (type, state, shareInfo, errorInfo, end) -> Void in

})
}


let pengyouquan = UIAlertAction(title: "朋友圈", style: UIAlertActionStyle.Default) { (action) -> Void in

}

let cancl = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
share.addAction(weibo)
share.addAction(qZone)
share.addAction(weixin)
share.addAction(pengyouquan)
share.addAction(cancl)
self.presentViewController(share, animated: true, completion: nil)
}

func photoCellDidClickMoreButton(photoCell: PhotoCell) {

let indexPath = tableView.indexPathForCell(photoCell)!

let alertController = UIAlertController(title: "更多", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

let report = UIAlertAction(title: "举报", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
let confirmReport = UIAlertController(title: "确认举报?", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let delete = UIAlertAction(title: "确认", style: UIAlertActionStyle.Destructive) { (alertAction) -> Void in
let report = ZLLReport.new()
report.reportedPhoto = self.photo
report.reporter = ZLLUser.currentUser()
let success = report.save()
if success{
ZLLViewModel.showHintWithTitle("举报成功", on: self.view)
}else{
ZLLViewModel.showHintWithTitle("举报失败", on: self.view)
}
}
let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
confirmReport.addAction(delete)
confirmReport.addAction(cancel)
self.presentViewController(confirmReport, animated: true, completion: nil)
}
alertController.addAction(report)

if photo.owner.objectId == ZLLUser.currentUser().objectId{
let delete = UIAlertAction(title: "删除图片", style: UIAlertActionStyle.Destructive) { (alertAction) -> Void in
let confirmDelete = UIAlertController(title: "确认删除?", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let delete = UIAlertAction(title: "确认", style: UIAlertActionStyle.Destructive) { (alertAction) -> Void in
let success = self.photo.delete()
self.photo.deleteInBackgroundWithBlock({ (success, error) -> Void in
if success{
self.performSegueWithIdentifier("deleteComplete", sender: nil)
ZLLViewModel.showHintWithTitle("删除成功", on: self.view)
self.myPopBackButtonClicked(nil)
}else{
print(error)
}
})
}
let cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
confirmDelete.addAction(delete)
confirmDelete.addAction(cancel)
self.presentViewController(confirmDelete, animated: true, completion: nil)
}
alertController.addAction(delete)
}

let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}

extension AllCommentsViewController: CommentCellProtocol{
func commentCellDidTapAuthorLabel(cell: CommentCell) {
let indexPath = tableView.indexPathForCell(cell)!
let comment = comments[indexPath.row]
let user = comment.author
performSegueWithIdentifier("showUser", sender: user)
}
}`

最佳答案

在您的 PhotoCell 类中,添加这段代码。

override func didMoveToSuperview() {
layoutIfNeeded()
}

关于ios - 首次加载时 UITableViewCell 变窄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32046228/

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