如何从tableview单元导航到viewController,它放在其他tableview Cell中?

时间:2018-03-19 08:27:04

标签: ios swift xcode

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

这里是代码,第二个表视图放在SegmentedCell中... 当我试图推动时,它无法进入下一个控制器..

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)



    }

}

}

2 个答案:

答案 0 :(得分:0)

在tableView2的didSelectrow方法中使用 get_bid() { let higest_bid_array:any[]; for(let i=0; i < this.crypto.length;i++) { higest_bid_array = this.crypto[i].highestBid; } return higest_bid_array; }

答案 1 :(得分:0)

我创建了一个与你类似的场景,这就是你如何使它发挥作用。

<强> 1。 View Hierarchy

enter image description here

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

  1. 外表viewView标签= 0
  2. 内部tableView标签= 1
  3. 2。现在为tableViews实施 UITableViewDataSource, UITableViewDelegate 方法。将tableViews的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中,您可以找到与tag中每个元素对应的attributes inspector属性,即

    enter image description here

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

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