autolayout在表格视图中不起作用

时间:2017-10-26 06:08:41

标签: ios swift

我正在我的表格视图中制作一个标题单元格,通常我对autolayout没有任何问题,并且我已经给出了像这样的图像和按钮的约束

enter image description here

但是当我运行应用程序时,所有图像和标签都附在左侧,我从不添加这样的前沿对齐

enter image description here

即使我更改了界面构建器中的约束,结果也完全相同。似乎自动布局仅对此表视图标题单元格不起作用或禁用。

这是表视图控制器的代码:

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

出了什么问题?

提前感谢:)

1 个答案:

答案 0 :(得分:0)

我的猜测是你将图像设置为超类的图像视图。将cell.imageView.image = fooImage替换为cell.profileImageView.image = fooImage