ios - 如何从放置在其他 TableView 单元格中的 TableView 单元格导航到 View Controller ?

标签 ios swift xcode

我已将 tableview2 放置在 tableview1 单元格中,现在当我单击 tableview2 单元格时,我需要导航到新的 viewController。请帮助我...我一整天都在挣扎:(

这是代码,第二个 TableView 放置在SegmentedCell中... 当我尝试插入时,它无法进入下一个 Controller ..

import UIKit
import XMSegmentedControl
import Alamofire
import SwiftyJSON

class segmentedCell: UITableViewCell, XMSegmentedControlDelegate, UITableViewDelegate, UITableViewDataSource{

let byndrColor : UIColor = UIColor( red: 224/255, green: 0/255, blue: 115/255, alpha: 1.0 )
let fontStyle = UIFont(name: "Lato-bold", size: 12)
@IBOutlet weak var segmentedControl: XMSegmentedControl!
@IBOutlet weak var feedTableView: UITableView!
var getApi = UIApplication.shared.delegate as! AppDelegate
 var course_id = String()
var materialListObjects = [MaterialsInSingleCourseGetSet]()
var assignmentExamAndQuizListObjects = [AssignmentAndExamsQuizGetSet]()




override func awakeFromNib() {
    super.awakeFromNib()


    feedTableView.delegate = self
    feedTableView.dataSource = self

    segmentedControl.delegate = self
    segmentedControl.segmentTitle = ["LATEST", "MATERIALS", "COURSEWORK", "PROGRESS"]
    segmentedControl.font = fontStyle!
    segmentedControl.selectedItemHighlightStyle = XMSelectedItemHighlightStyle.BottomEdge
    segmentedControl.backgroundColor = UIColor.white
    segmentedControl.tint = UIColor.black
    segmentedControl.highlightTint = byndrColor
    segmentedControl.highlightColor = byndrColor
    segmentedControl.edgeHighlightHeight = 2

    segmentedControl.selectedSegment = 0

 let share = UIApplication.shared.delegate as! AppDelegate

    materialListObjects = share.materialListInSingleCourse as! [MaterialsInSingleCourseGetSet]
    assignmentExamAndQuizListObjects = share.assignmentsExamsAndQuizListInSingleCourse as! [AssignmentAndExamsQuizGetSet]


    // Initialization code
}

func xmSegmentedControl(xmSegmentedControl: XMSegmentedControl, selectedSegment: Int) {

    if xmSegmentedControl == segmentedControl {
        print("SegmentedControl1 Selected Segment: \(selectedSegment)")
        switch  segmentedControl.selectedSegment
        {
        case 0:

           feedTableView.reloadData()
        case 1:
           feedTableView.reloadData()
        case 2:
            feedTableView.reloadData()
        case 3:
            feedTableView.reloadData()
        default :
            break
        }
    }
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if segmentedControl.selectedSegment == 0
    {
    return 0
    }
    else
    if segmentedControl.selectedSegment == 1
    {
        return materialListObjects.count
    }
    else
    if segmentedControl.selectedSegment == 2
    {
        return assignmentExamAndQuizListObjects.count

    }
    else
    {
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if segmentedControl.selectedSegment == 0
    {

        let cell = Bundle.main.loadNibNamed("TypeOneCell", owner: self, options: nil)?.first as! TypeOneCell
        return cell
    }
    else
        if segmentedControl.selectedSegment == 1
        {

            if materialListObjects[indexPath.row].type == "file"
            {
                let cell = Bundle.main.loadNibNamed("materialCellOne", owner: self, options: nil)?.first as! materialCellOne
                cell.materialNameLabel.text = materialListObjects[indexPath.row].title
                let image = materialListObjects[indexPath.row].title
                cell.contentImage.image = image.documentType(givenType: image)

                return cell
            }else
            {
                let cell = Bundle.main.loadNibNamed("materialCellTwo", owner: self, options: nil)?.first as! materialCellTwo
                cell.materialNameLabel.text = materialListObjects[indexPath.row].title

                cell.contentImage.image = #imageLiteral(resourceName: "material_hyperlink")

                return cell
            }


        }
        else
            if segmentedControl.selectedSegment == 2
            {
                let cell = Bundle.main.loadNibNamed("CourseWorkCell", owner: self, options: nil)?.first as! CourseWorkCell
                print("assignment title : \(assignmentExamAndQuizListObjects[indexPath.row].title)")
                cell.titleLabel.text = assignmentExamAndQuizListObjects[indexPath.row].title
                if assignmentExamAndQuizListObjects[indexPath.row].type == ""
                {
                    cell.contentImage.image = #imageLiteral(resourceName: "assignment_large")
                }else
                {
                    cell.contentImage.image = #imageLiteral(resourceName: "exam_inline")
                }
                var time = assignmentExamAndQuizListObjects[indexPath.row].start
                time =  time.dateRange(dateString: time)
                time = time.days(givenDate: time)
                cell.timeLabel.text = time
                return cell
            }
            else
                if segmentedControl.selectedSegment == 3
                {
                    let cell = Bundle.main.loadNibNamed("TypeOneCell", owner: self, options: nil)?.first as! TypeOneCell
                    return cell
                }
                else
                {
                    let cell = Bundle.main.loadNibNamed("TypeOneCell", owner: self, options: nil)?.first as! TypeOneCell
                    return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if segmentedControl.selectedSegment == 2
    {
     return 70
    }
    else
    {
    return 100
    }
}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect.zero)
    let label = UILabel(frame: CGRect(x: 8, y: 8, width: 150, height: 20))
    view.addSubview(label)
    label.font = UIFont(name: "Lato-Heavy", size: 17)

    if segmentedControl.selectedSegment == 1
    {
    switch section {
    case 0:
        label.text = "All Materials"
    case 1:
        label.text = "From Your Courses"
    default:
        break
        }
    }
    else
    if segmentedControl.selectedSegment == 2
    {
        switch section {
        case 0:
            label.text = "All CourseWork"
        case 1:
            label.text = "From Your Courses"
        default:
            break
        }
    }
    else
    {

    }




    return view
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

//如何从这里执行

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if segmentedControl.selectedSegment == 1
    {
        let storyboard = UIStoryboard(name: "Main", bundle : nil)
        let nextViewController = storyboard.instantiateViewController(withIdentifier: "QuickLook") as! QuickLook


        if materialListObjects[indexPath.row].type == "url"
        {
            nextViewController.id = materialListObjects[indexPath.row].body
            nextViewController.type = "url"
        }
        else
        {
            nextViewController.id = materialListObjects[indexPath.row].id
        }
        nextViewController.course_id = String(describing: materialListObjects[indexPath.row].course_id)
        let naviControl = UINavigationController(rootViewController: nextViewController)
        naviControl.pushViewController(nextViewController, animated: true)



    }

}

}

最佳答案

我创建了一个与您类似的场景,这就是让它发挥作用的方法。

<强>1。 查看层次结构

enter image description here

我使用 tag 属性来唯一标识两个 UITableViews,即

  1. 外部 tableView 标签 = 0
  2. 内部 tableView 标记 = 1

2. 现在为两个 tableView 实现 UITableViewDataSource、UITableViewDelegate 方法。将两个 tableView 的 dataSourcedelegate 设置为 ViewController

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if tableView.tag == 0
    {
        return 1
    }
    else if tableView.tag == 1
    {
        return 5
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if tableView.tag == 0
    {
        return tableView.dequeueReusableCell(withIdentifier: "outercell", for: indexPath)
    }
    else if tableView.tag == 1
    {
        return tableView.dequeueReusableCell(withIdentifier: "innercell", for: indexPath)
    }
    return UITableViewCell()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    if tableView.tag == 1
    {
        //TODO: Write your code for navigating to another ViewController here
        print("Inner cell tapped")
    }
}

编辑:

Interface Builder中,您可以在attributes Inspector中找到与每个元素对应的tag属性,即

enter image description here

对于outer tableView,将其设置为0,对于inner tableView,将其设置为1

如果您仍然遇到任何问题,请告诉我。快乐编码..🙂

关于ios - 如何从放置在其他 TableView 单元格中的 TableView 单元格导航到 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358530/

相关文章:

ios - iOS 中的 Facebook 深层链接

ios - 如何删除NSUserDefaults内部的NSMutableDictionary内部的对象

swift - 如何在命令行上使用和运行 Swift 2.3

objective-c - RKObject 子类化

ios - 如何使用animationWithDuration和Chameleon Gradients?

ios - iOS 应用程序共享框架吗?

ios - 我的视频离开屏幕,但留在它的 UIView 中

ios - UITableViewController 中的 UITextField

ios - 选择了错误的 iOS 配置文件

ios - 在 Swift 中,如何直接修改存储在字典中的数组?