Swift 4 - 以编程方式在AutoLayout UITableViewCell中切割的多行标签

时间:2017-10-13 10:18:46

标签: ios swift uitableview autolayout

请在下面找到我的代码,以使用AutoLayout布局UITableViewCell的UI编程:

org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:353)
           at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
           at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
           at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
           at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:344)
           at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
           at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
           at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
           at java.util.concurrent.FutureTask.run(Unknown Source)
           at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
           at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
           at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
           at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.IllegalStateException: failed to create SFTP Session
           at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:393)
           at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:57)
           at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:433)
           ... 24 more
    Caused by: java.lang.IllegalStateException: failed to connect
           at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:273)
           at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:388)
           ... 26 more
        Caused by: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 Too many authentication failures for *username*
        at com.jcraft.jsch.Session.read(Session.java:1004)
        at com.jcraft.jsch.UserAuthPassword.start(UserAuthPassword.java:91)
        at com.jcraft.jsch.Session.connect(Session.java:470)
        at com.jcraft.jsch.Session.connect(Session.java:183

这就是我想要实现的目标: Username Label stick to contentView

在达到我的目标之前,我尝试了另一个实现。实施如下:

// Profile Pic
profileView.translatesAutoresizingMaskIntoConstraints = false
profileView.widthAnchor.constraint(equalToConstant: commentImageSize + 4).isActive = true
profileView.heightAnchor.constraint(equalToConstant: commentImageSize + 4).isActive = true
profileView.topAnchor.constraint(equalTo: topAnchor, constant: borderSize).isActive = true
profileView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: borderSize).isActive = true

profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.widthAnchor.constraint(equalToConstant: commentImageSize).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: commentImageSize).isActive = true
profileImageView.topAnchor.constraint(equalTo: profileView.topAnchor, constant: 2).isActive = true
profileImageView.leadingAnchor.constraint(equalTo: profileView.leadingAnchor, constant: 2).isActive = true

// Time Ago Label
timeAgoLabel.translatesAutoresizingMaskIntoConstraints = false
timeAgoLabel.topAnchor.constraint(equalTo: usernameLabel.topAnchor).isActive = true
timeAgoLabel.leadingAnchor.constraint(equalTo: usernameLabel.trailingAnchor, constant: 0)
timeAgoLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -borderSize).isActive = true

// Username Label
usernameLabel.translatesAutoresizingMaskIntoConstraints = false
usernameLabel.leadingAnchor.constraint(equalTo: profileView.trailingAnchor, constant: borderSize).isActive = true
usernameLabel.trailingAnchor.constraint(equalTo: timeAgoLabel.leadingAnchor, constant: -borderSize).isActive = true
// *****
// (Not work)
// usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true
// (Work)
usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true
// *****

// Comment Label
commentLabel.translatesAutoresizingMaskIntoConstraints = false
commentLabel.leadingAnchor.constraint(equalTo: usernameLabel.leadingAnchor).isActive = true
commentLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -borderSize).isActive = true
commentLabel.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor, constant: 2).isActive = true
commentLabel.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -borderSize).isActive = true

但是,由于未知原因,如果使用此实现,则会触发多行注释标签。 Username Label stick to Profile Pic

使用以下代码后,情况完美无缺:

usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true

两行代码在我的期望中应该具有相同的效果。我想知道为什么我原来的implmentation不能按预期工作。

感谢。

1 个答案:

答案 0 :(得分:0)

  

我想知道为什么我原来的implmentation不起作用   预期

此代码

usernameLabel.topAnchor.constraint(equalTo: profileView.topAnchor).isActive = true 

表示您将usernameLabel的顶部与profileView的顶部对齐,我相信您也知道。

这段代码:

usernameLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: borderSize).isActive = true

表示您将约束置于单元格或特定单元格contentView

现在,您需要使多线UILabel正确放入单元格中的方法是为其超级视图的顶部和底部提供约束。这就像使用UIScrollView一样,你需要对每一方都有适当的约束。

同样,在你的第一个代码中,你基本上错过了一个对superview top有约束力的重要约束。您可以根据需要在Storyboard / Interface Builder上进行实验。