gpt4 book ai didi

ios - 自动布局在表格 View 中不起作用

转载 作者:行者123 更新时间:2023-11-30 12:03:23 27 4
gpt4 key购买 nike

我正在我的表格 View 中创建一个标题单元格,通常我对自动布局没有问题,并且我对图像和按钮进行了约束,如下所示

enter image description here

但是当我运行应用程序时,所有图像和标签都附加到左侧,我从来没有像这样添加前缘对齐

enter image description here

即使我更改界面生成器中的约束,结果也完全相同。看来自动布局不起作用或仅对此 TableView 标题单元格禁用。

这是 TableView Controller 的代码:

class MediaDetailTableViewController: UITableViewController {

var media: Media!
var currentUser : User!
var comments = [Comment]()




override func viewDidLoad() {
super.viewDidLoad()

navigationItem.title = "Photo"

tableView.allowsSelection = false
tableView.estimatedRowHeight = StoryBoard.mediaCellDefaultHeight
tableView.rowHeight = UITableViewAutomaticDimension

comments = media.comments
tableView.reloadData()

}





// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + comments.count
// 1 for the media image detail itself + the number of comments
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


if indexPath.row == 0 {
// media row
let cell = tableView.dequeueReusableCell(withIdentifier: StoryBoard.mediaCell, for: indexPath) as! MediaTableViewCell

cell.currentUser = currentUser
cell.media = media

return cell

} else {
// comment row
let cell = tableView.dequeueReusableCell(withIdentifier: StoryBoard.commentCell, for: indexPath) as! CommentCell

cell.comments = comments[indexPath.row - 1]

return cell


}



}


// to adjust the height of media cell
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return StoryBoard.mediaCellDefaultHeight
}




// for media header cell
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: StoryBoard.mediaHeaderCell) as! MediaHeaderCell

cell.currentUser = currentUser
cell.media = media
cell.backgroundColor = UIColor.white

return cell



}

// set media header height
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return StoryBoard.mediaHeaderHeight
}




}

这是单元格的代码:

class MediaHeaderCell: UITableViewCell {

@IBOutlet weak var profileImageView : UIImageView!
@IBOutlet weak var usernameButton : UIButton!
@IBOutlet weak var followButton : UIButton!


// the currentUser and Media properties below, will be directed from NewsFeed Table View Controller

var currentUser : User!
var media : Media! {
didSet {

// if there is someone logged in and he posts the media,then updateUI

if currentUser != nil {
updateUI()
}

}
}

var cache = SAMCache.shared()



func updateUI() {

profileImageView.image = #imageLiteral(resourceName: "icon-defaultAvatar")

let cacheKey = "\(self.media.createdBy.uid)-headerImage"



// check, if the image has already been downloaded and cached then just used the image, otherwise download from firebase storage



if let image = cache?.object(forKey: cacheKey) as? UIImage {

self.profileImageView.image = image

} else {
// they will be closure retain cycle if we use the method below without [weak var], because there are 2 strong reference between Class and the closure (user - profileImage).

media.createdBy.downloadProfilePicture { [weak self] (image, error) in


if let image = image {
self?.profileImageView.image = image
self?.cache?.setObject(image, forKey: cacheKey)
} else if error != nil {
print(error!)
}


}
}




profileImageView.layer.cornerRadius = profileImageView.bounds.width / 2.0
profileImageView.layer.masksToBounds = true

usernameButton.setTitle(media.createdBy.username, for: [])


followButton.layer.borderWidth = 1
followButton.layer.cornerRadius = 2.0
followButton.layer.borderColor = followButton.tintColor.cgColor
followButton.layer.masksToBounds = true


// check the other users are already followed or not, if yes then follow button shall be shoed up

if currentUser.follows.contains(media.createdBy) || media.createdBy.uid == currentUser.uid {
followButton.isHidden = true
} else {
followButton.isHidden = false
}
}
}

出了什么问题?

提前致谢:)

最佳答案

我的猜测是您将图像设置为父类(super class)的 ImageView 。将 cell.imageView.image = fooImage 替换为 cell.profileImageView.image = fooImage

关于ios - 自动布局在表格 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46946944/

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