i am making a header cell in my table view, usually i have no issue with autolayout, and i have given constraints to the image and button like this
but when i run the app, all images and labels are attached to the left side, i never add leading edges alignment like this
even if i change the constraints in the interface builder, the result is just exactly the same. it seems that the autolayout doesn't work or disabled for this table view header cell only.
here is the code of the table view 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
}
}
here is the code of the cell :
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
}
}
}
what went wrong ?
thanks in advance :)