- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当用户离开并返回时,如何防止 View Controller 从提要的顶部启动?
基本上,我有主要的VC和详细的VC。当用户选择一个单元格时,它应该跳转到详细的VC。如果她/他回去,她/他应该回到原来的地方。
我知道每次 VC 加载时我的代码都会调用“重新加载数据”,但是如果我不调用该方法,我还有什么其他选择?
这是我的主要 Storyboard的图像(如果有帮助的话)。主 VC(左)是 feed tableView,用户可以在其中点击单元格。当他/她点击单元格时,它会“跳转”到评论表 VC(右)。当他/她完成评论后,她/他可以返回到主 VC 并继续查看 feed。 (理想情况下,除了它不断从最新的帖子加载,而不是让用户回到她/他在提要中的位置)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostCell
let post: PostModel
post = postList[indexPath.row]
func set(post: PostModel) {
ImageService.downloadImage(withURL: post.author.patthToImage) { image in
cell.profileImage.image = image
}
}
set(post: postList[indexPath.row])
cell.descriptionLabel.numberOfLines = 0 // line wrap
cell.descriptionLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.descriptionLabel.text = post.message
cell.authorLabel.text = post.author.username
cell.timeLabel.text = post.createdAt.calendarTimeSinceNow()
//takes care of post image hidding and showing
if self.postList[indexPath.row].pathToImage != "" {
cell.postImage.isHidden = false
cell.postImage?.downloadImage(from: self.postList[indexPath.row].pathToImage)
} else {
cell.postImage.isHidden = true
}
if cell.postImage.isHidden == true {
cell.postImage.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post: PostModel
post = postList[indexPath.row]
myIndex = indexPath.row
myPost = post.postID!
performSegue(withIdentifier: "segue", sender: self)
print(myIndex)
print(post.postID)
}
override func viewDidLoad() {
super.viewDidLoad()
beginBatchFetch()
}
func beginBatchFetch() {
fetchingMore = true
fetchPosts { newPosts in
self.postList.append(contentsOf: newPosts)
self.endReached = newPosts.count == 0
self.fetchingMore = false
self.tableViewPost.reloadData()
}
}
func fetchPosts(completion: @escaping(_ postList:[PostModel])->()) {
ref = Database.database().reference().child("posts")
var queryRef:DatabaseQuery
let lastPost = self.postList.last
if lastPost != nil {
let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
queryRef = ref.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp).queryLimited(toLast:20)
} else {
queryRef = ref.queryOrdered(byChild: "timestamp").queryLimited(toLast:20)
}
queryRef.observeSingleEvent(of: .value, with: { snapshot in
var tempPosts = [PostModel]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let username = author["username"] as? String,
let fullname = author["fullname"] as? String,
let patthToImage = author["patthToImage"] as? String,
let url = URL(string:patthToImage),
let pathToImage = dict["pathToImage"] as? String,
let likes = dict["likes"] as? Int,
let postID = dict["postID"] as? String,
let message = dict["message"] as? String,
let genre = dict["genre"] as? String,
let timestamp = dict["timestamp"] as? Double {
let userProfile = UserProfile(uid: uid, fullname: fullname, username: username, patthToImage: url)
let post = PostModel(genre: genre, likes: likes, message: message, pathToImage: pathToImage, postID: postID, userID: pathToImage, timestamp: timestamp, id: childSnapshot.key, author: userProfile)
tempPosts.insert(post, at: 0)
}
}
//first two
self.postList = tempPosts
self.tableViewPost.reloadData()
// return completion(tempPosts)
})
最佳答案
正如 Matt 指出的那样,问题在于您正在转至detailVC,然后转回原始VC。这将创建原始 VC 的新实例。
在带有 TableView 的 VC 中,您应该做的是在选择单元格时实例化并显示目标 View Controller 。因此,您应该将 performSegue(withIdentifier: "segue", sender: self)
替换为以下内容:
let storyboard = UIStoryboard(name: "Main", bundle: .main)
var destinationVC = (storyboard.instantiateViewController(withIdentifier: "DestinationVC") as! DestinationViewController)
present(destinationVC!, animated: ture, completion: nil)
注意: View Controller Storyboard标识符可以在界面构建器中设置。因此,如果您想使用这一行:var destinationVC = (storyboard.instantiateViewController(withIdentifier: "DestinationVC") as!DestinationViewController)
,您首先必须在界面构建器中设置 Storyboard ID :
现在,在您的目标 View Controller 中,您希望使用 dismiss
方法来关闭所呈现的 View Controller ,而不是在按下完成按钮时进行继续操作。
class DestinationViewController: UIViewController {
@IBAction func backButtonPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
现在,带有 TableView 的原始 VC 保留在内存中,而目标 VC 则显示在其上。当用户按下“后退”按钮时,它将关闭目标 VC,并且原始 VC 应重新出现在您离开时的位置。
关于swift - Tableview 总是回到顶部?离开后到 "detailed"vc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58260379/
我最后一次使用C++是在它无法被管理之前。不过最近,我从 Java 回来,看到现在可以管理 C++ 了! 没过多久我就意识到gcnew 和^ 的用途。但是,我对容器有点卡住了。 如何创建一个容器,其元
我离开 Microsoft 堆栈已有一段时间了,专注于 Linux、开源内容和 PHP 中的 Web 开发。我曾经在 Dev Studio(所有 C 和 C++)中在 Windows 上进行一些桌面应
在我的程序中有两个 View Controller 。第一个有表格 View 。当我单击一个单元格时,相关的炎症会显示在第二个 View Controller 中。它运作良好。 当我返回到第一个 Vi
我使用 AVAssetWriter 和 CMSampleBuffer 数据(来自视频、音频输入)录制视频(.mp4 文件)。 在录制时我想处理帧,我正在将 CMSampleBuffer 转换为 CII
在 python 中有两种不同的离开循环的选项。 continue 将您带回到循环的开头,break 就像一个电灯开关,它会在脚本运行的剩余时间内切断循环。我的问题是我有一个 while True 循
我是 Git 的新手,我正试图恢复到 SourceTree 中的先前提交。我右键单击要还原到的提交,然后单击 checkout 。它给了我一个提示,说我的工作副本将成为一个独立的头。这是什么意思,这是
所以我决定在离开几年后,为了我的一些个人项目重新使用 Ruby on Rails。我想知道的是,找出 Rails 中的新功能的最佳资源是什么?自从 1.2 是新的以来,我什至没有真正接触过 Rails
我的项目有两个部分。第一部分是在 Storyboard中制作的,第二部分是 SKView。如何从 SKView 中的第二部分返回到主 UIView? 最佳答案 创建自定义 ViewController
所以我在大约四次提交前对我的项目做了一个糟糕的改变。我了解到我可以恢复到之前描述的状态 here ,并通过依次检查以前的提交(并在我的设备上测试它们),我已经确定了问题发生的位置。 现在我想回到坏改变
我想知道,在 Canvas 的 commandAction 方法中,如何让我的命令按钮回到 MIDlet 的开始? (基本上重新开始)。 当按键触发时,我将它带到一个新的列表页面。在该页面上,我有一个
我想知道是否可以使用 intro.js 返回到下一行。我尝试了\n 和其他类似的东西,但它们中的任何一个都有效并且不可能在文档中找到类似的东西。有谁知道这是否可能? 最佳答案 正确的做法是像这样使用
这是关于我发现我的应用程序面临的一个反复出现的问题,它与使用几个 DialogFragment 相关。我主要针对平台级别 8 设备,因此要使用 DialogFragments,我必须使用兼容性库。 每
我有一个 uiview 的问题,它放置在 Storyboard的一个位置,在应用程序启动后,我将 uiview 移动到第二个位置,并使用代码中的按钮进行动画处理。 int alpha = -212;
我有 Controller B,它使用委托(delegate)模式将数据发送回 Controller A,但由于某种原因我的 segue 没有触发。 是否有什么东西阻止我的 segue 被触发?我将如
我已经找到了处理除我需要的之外的所有内容的解决方案。这是场景 就像在 GMail 中一样 - 主要内容呈现在 iframe 中。单击主页上的链接会指向 iframe。这效果很好,而且无缝。此时,如果我
我有一个 RCP 程序,带有需要登录的启动屏幕。 我想制作一个注销按钮。通过单击此按钮,用户应该返回到初始屏幕,因此他必须重新登录.. 这可能吗? 提前致谢。 最佳答案 如果您使用org.eclips
我有一个数据框: df = pd.DataFrame({'Section': [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6],
是否可以将元素 $("p") 返回到应用 mouseenter() 方法之前的确切颜色?或者我是否需要知道 mouseenter() 之前的颜色,然后使用 mouseleave() 应用该颜色?我希望
在 Matlab R2016b 中,显示某些数据类型的变量会显示有关该类型的信息。当通过不带最终分号键入变量来显示变量时会发生这种情况(使用 disp 函数时不会发生这种情况)。 比较例如: Matl
是否可以告诉 RSpec::Mocks 为一组值 stub 一个方法,否则回退到原始方法?例如: File.stub(:exist?).with(/txt/).and_return(true) Fil
我是一名优秀的程序员,十分优秀!