Swift:如何在 UITableViewController(包含 UICollectionView)中使用“didSelectItemAtIndexPath”?

2024-04-30

我有一个UITableViewController, 在 - 的里面TableViewCell, 它是UICollectionView。我想传递来自CollectionViewCell to a DetailViewController当用户点击单元格时。我从CollectionViewCell to the DetailViewController,并使用了didSelectItemAtIndexPath在 - 的里面TableViewCell( 其中包含CollectionView),并且它有效。

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

var posts1: [Posts] = [Posts]()
var posts2: [Posts] = [Posts]()
var categories: [Category] = [Category]()
var selectedPost1: Posts?

@IBOutlet weak var theCollectionView: UICollectionView!


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return posts1.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let collectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell

        let imageUrlString: String = posts1[indexPath.row].postThumbnailUrlString
        let imageUrl: NSURL = NSURL(string: imageUrlString)!
    //Give Images A round cornor

    let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
        size: collectionViewCell.postImageView.frame.size,
        radius: 20.0
    )

    collectionViewCell.postImageView.af_setImageWithURL(imageUrl, filter: filter)

    collectionViewCell.postTitleLabel.text = posts1[indexPath.row].postTite
return collectionViewCell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("selected")
    self.selectedPost1 = self.posts1[indexPath.row]
}

它可以打印“选定”这意味着数据已经存储在self.selectedPost1。但我不能使用prepareForSegue在这个类中,因为它只能用于ViewController。有人告诉我要实施UICollectionViewDelegate in my HomeTableViewController,并调用该函数didSelectedItemAtIndexPath in the HomeTableViewController, 像这样:

import UIKit

class HomePageTableViewController: UITableViewController,UICollectionViewDelegate {
    let sections: NSArray = ["latest news", "good news"]
    var posts1: [Posts] = [Posts]()
    var selectedIndexPath: NSIndexPath?

override func viewDidLoad() {
    super.viewDidLoad()


}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  if section < sections.count{
        return sections[section] as? String
    }

    return nil

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0 && indexPath.section == 0 {
        let tableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell

        tableViewCell.getCategories()
     //   tableViewCell.scrollToNextCell()
     //   tableViewCell.startTimer()


        return tableViewCell

    }
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("goToDetail", sender: TableViewCell())
    print("selected")
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let tableViewCell1 = sender as? TableViewCell,
    let postDetailPage = segue.destinationViewController as? DetailViewController{

        let selectedPosts = tableViewCell1.selectedPost1

        postDetailPage.selectedPost?.selectedPost1 = selectedPosts

}

但是,那didSelectedItemAtIndexPath无法调用,没有打印。我不知道为什么?

这是我的DetailViewController:

import UIKit

class DetailViewController: UIViewController {
@IBOutlet weak var postImageView: UIImageView!

var posts: [Posts] = [Posts]()
var selectedPost: TableViewCell?

override func viewDidLoad() {
    super.viewDidLoad()

    if let post = selectedPost?.selectedPost1{
        let thumbnailUrlString = post.postThumbnailUrlString
        let imageUrl: NSURL = NSURL(string: thumbnailUrlString)!
        print(thumbnailUrlString)
        postImageView.af_setImageWithURL(imageUrl)
    }
}

我还需要实施viewDidLoad or viewDidAppear?

我已经被这个问题困扰好几天了?需要一些关于如何从 a 进行 Segue 的建议UICollectionViewCell(这是在一个UITableViewCell)到一个新的UIViewController.


你的做法是正确的,但你犯了一个错误。尝试实施UICollectionViewDelegate method didSelectItemAtIndexPath也在里面TableViewCell并将其从HomePageTableViewController.

现在声明一个协议,然后在里面创建它的实例TableViewCell并在中实施该协议HomePageTableViewController,之后在didSelectItemAtIndexPath使用该委托实例来调用这样的方法。

protocol PostDelegate {
    func selectedPost(post: Posts)
}

现在在里面创建它的实例TableViewCell并在中调用这个委托方法didSelectItemAtIndexPath.

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
     var posts1: [Posts] = [Posts]()
     var posts2: [Posts] = [Posts]()
     var categories: [Category] = [Category]()
     var selectedPost1: Posts?  
     var postDelegate: PostDelegate?

     func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
         postDelegate?. selectedPost(self.posts1[indexPath.row])
     }
}

现在实施这个PostDelegate里面的协议HomePageTableViewController

class HomePageTableViewController: UITableViewController,UICollectionViewDelegate { 


     //Your code 

     func selectedPost(post: Posts) {
         //You will get your post object here, do what you want now
     }
}

Note: Inside cellForRowAtIndexPath不要忘记设置代表postDelgate和你的HomePageTableViewController像这样

tableViewCell.postDelegate = self

Edit:

func selectedPost(post: Posts) {
    //You will get your post object here, do what you want now
    self.performSegueWithIdentifier("goToDetail", sender: post)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let post = sender as! Posts
    let postDetailPage = segue.destinationViewController as? DetailViewController
    postDetailPage.passPost = post
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:如何在 UITableViewController(包含 UICollectionView)中使用“didSelectItemAtIndexPath”? 的相关文章

随机推荐

  • 当 contenteditable 更改时触发事件

    当div值改变时 如何触发事件 div class changeable Click this div to edit it div 因此 当其内容发生变化时 我想创建警报和 或执行其他操作 changeable text change f
  • redirect_uri 以及如何在 SoundCloud 上托管callback.html?

    我正在尝试从笔记本电脑上的本地 HTML 页面访问 Soundcloud 我被困在将 callback html 托管为redirect uri 的部分 我尝试运行的脚本是来自 Soundcloud 文档页面的基本身份验证 JavaScri
  • 如何自定义处理 Quarkus 中未找到的资源/URL?

    我对 Quarkus 很陌生 我想知道如何覆盖提供错误日志的默认 404 页面 或者如何巧妙地将所有无法识别的 URL 重定向到 META INF recourses 目录中的自定义 HTML 使用 quarkus 0 20 你可以创建一个
  • Readelf 报告程序是共享库而不是可执行文件

    使用独立的 Android NDK r10e 工具链 使用 toolchain x86 clang3 6 开关构建 出现这种奇怪的行为 交叉编译的环境变量已设置在运行makefile之前 SYSROOT指向Android工具链位置 CXX等
  • 如何将自定义 HTML 代码放入 WordPress 网站的标题中 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 这是我的网站 我需要放置 html
  • DataGridView 的替代品有哪些? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我们希望在 Winforms 应用程序中替换 DataGridView 我们应该评估哪些替代方案 商业或
  • 从 Parse.com 获取 NSString 并将其粘贴到 UILabel 中

    我想从 Parse com 获取 NSString 并将其粘贴到我的 iOS 应用程序中的标签中 有谁知道该怎么做 我遇到了很大的问题 提前致谢 反问题是 你想要得到哪个 NSString 我认为这是您想要显示的 PFObject 的某些属
  • 为什么字符串中的反斜杠(\)在控制台中给出错误

    我有一个像这样的字符串 C projects cisco iwan staging enc enterprise network controller ui plugins iwan 当我粘贴到console然后按回车键 它给出以下错误 U
  • jQuery Mobile/MVC:使用 RedirectToAction 更改浏览器 URL

    我的第一篇文章 当我使用 RedirectToAction 时 浏览器中的 url 不会改变 我怎样才能实现这个目标 在使用 Web 表单 10 多年后 我将切换到 ASP NET MVC 3 0 也使用 jQuery Mobile 我已经
  • 我收到“无法解析模块`react

    i am getting the below error 开发服务器返回响应错误码 500 URL http 10 0 2 2 8081 index android bundle platform android dev true hot
  • Android NSD 未发现所有服务

    我正在尝试使用 Android 本机服务发现来运行应用程序 但有时当我运行该应用程序时 它不会发现我的网络中的所有服务 我正在运行代码https github com joeluchoa nsd https github com joelu
  • ExitCodeGenerator 和 System.exit(0) 之间的区别

    我最近了解到关闭 Spring Boot 应用程序的正确方法是 public class Application Bean public ExitCodeGenerator exitCodeGenerator return new Exit
  • 什么时候适合使用Lua这样的嵌入式脚本语言

    我玩 魔兽世界 大约有两年了 我对用来编写插件的 Lua 很好奇 由于到目前为止我读到的有关 Lua 的内容都是 快 轻 和 这太棒了 所以我想知道如何以及何时使用它 您需要在系统中嵌入像 Lua 这样的脚本语言的典型情况是什么 当您需要最
  • 将控制器工厂添加到 ASP MVC

    我对工作中的一个大型项目有一个设计想法 我想我已经弄清楚了 但我真的很想得到一些关于a 总体想法和b 我提议的实现的反馈 基本想法很简单 我想创建一个 ASP MVC 应用程序 将来可以使用其他控制器和视图进行扩展 而无需重新编译代码 这个
  • 同时冻结第 1 行和 A 列

    我想在 Excel 2010 中同时 冻结 第 1 行和 A 列 这可能吗 选择单元格 B2 并单击 冻结窗格 这将冻结第 1 行和 A 列 为了便于将来参考 在 Excel 中选择 冻结窗格 将冻结所选单元格上方的行以及所选单元格左侧的列
  • 如何使用分页库在回收器视图中添加日期分隔符?

    经过大量搜索 我知道使用常规适配器是可能的 但我不知道如何使用分页库来做到这一点 我不需要代码 只是一个线索 Example 要添加分隔符 您基本上有两个选择 基于视图 您显式地将分隔符作为 项目 包含在列表中 并为这些分隔符定义新的视图类
  • 在Eclipse中添加注释掉代码的快捷按钮

    只是想知道是否有一种方法可以在 Eclipse 编辑器中添加一个按钮 就像在 Visual Studio 中一样 在 Java 视图 中快速注释或取消注释选定的代码块 Using the keyboard shortcut isn t ea
  • React 中大括号的使用

    我正在尝试学习 React 我在使用花括号时遇到问题 JSX 和 JS 之间使用大括号的区别 在下面的代码中 大括号 1 表示 现在是 JS 为什么有花括号 2 它已经在花括号区域内了吗 var React require react va
  • 如何在 suave webpart 中设置 Json 响应

    我从 Suave 和 F 开始 我正在尝试在我的 web 部件中传递一个 json 序列化对象以在我的响应中获取它 在 php 中我有这个 player1Key hdegftzj25 gameKey aegfhzkfszl
  • Swift:如何在 UITableViewController(包含 UICollectionView)中使用“didSelectItemAtIndexPath”?

    我有一个UITableViewController 在 的里面TableViewCell 它是UICollectionView 我想传递来自CollectionViewCell to a DetailViewController当用户点击单