将两个表视图添加到Xcode中的主视图 [英] Adding two Table Views to the main View in Xcode

查看:101
本文介绍了将两个表视图添加到Xcode中的主视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将两个Table View放在我的main.storyboard上。然后我为它们创建了两个新的.swift类文件:VideoTableViewController和LinkTableViewController。我如何链接这两个?看起来我可能做错了,因为我读过我需要将委托链接到表视图所在的View。我希望它链接到我为Table View创建的控制器。
我要做的是并排放置两个列表,一个带有视频链接,另一个带有网络链接。我已经有了数组中的数据。我只需要将数据放入正确的表格视图的单元格中。

I placed two Table View's onto my main.storyboard. I then created two new .swift class files for them: VideoTableViewController and LinkTableViewController. How do I link the two? It looks like I may have done this wrong because I read I need to have the delegate linked to the View that the Table View is on. I want it linked to the controller I created for the Table View. What I am trying to do is have two lists side by side, one with video links and another with web links. I already have the data in arrays. I just need to get that data into the cells of the proper table view.

我注意到我可以将表视图控制器放到故事板上,但我不认为那个
是我想要的,因为它是一个完全不同的观点。我希望这些视图在同一视图上。

I noticed I can drop a Table View Controller onto the storyboard, but i don't think that 's what I want since it's an entirely different view. I would like for these to be on the same view.

VideoTableViewController:

VideoTableViewController:

import Foundation
import UIKit

class VideoTableViewController: UITableViewController {
@IBOutlet weak var videoTable = UITableView()

var videos: [Video] = []

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {

    //disable header
    tableView.contentInset = UIEdgeInsetsMake(-30, 0, -20, 0);

    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Component") as UITableViewCell

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Component")
    }

    //cell.textLabel.text = "Baking Soda"
    //cell.detailTextLabel.text = "1/2 cup"

    return cell
}

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    return 1
}

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.videos.count
}

func setVideos(videos:[Video]) {
    self.videos = videos
}
}

主ViewController:

The main ViewController:

这会填充对象数组,然后将数组传递给正确的表控制器。

This fills the arrays of objects then passes the array to the proper table controller.

import UIKit

class ViewController: UIViewController {

//arrays to hold objects
var videos  :[Video] = []
var links   :[Link] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.fillArrays()

    //instantiate TableViewController
    let videoTable = VideoTableViewController()
    let linkTable = LinkTableViewController()
    videoTable.setVideos(videos)
    linkTable.setLinks(links)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func fillArrays() {
    let file = File()
    let path = "/Users/me/file.rtf"

    if (file.exists(path)) {                                        //if file exists
        let read :String? = file.read(path)                         //read the file
        let content :String = read!                                 //get file content
        let lines = content.componentsSeparatedByString("\n")       //explode content on line breaks

        var lineItem :[String]                                      //to hold each item of line explode
        for line in lines {                                         //loop through lines
            lineItem = line.componentsSeparatedByString(",")        //explode line on commas

            //instantiate an object for the line, initialize it, and append the object to its array
            switch lineItem[0] {
            case "L":
                self.links += Link(image: lineItem[0], path: lineItem[1])
            case "V":
                var duration :Int = 100
                self.videos += Video(image: lineItem[1], path: lineItem[2], title: lineItem[3], duration: duration)
            default:
                break
            }
        }
    }
}
}

那么,我是否会这样做?显然我正在做一件奇怪的事情,因为我找不到Googling上的直接信息。

So, am I going about this wrong? Apparently it is an odd thing I am doing since I can't find much straight forward info on it Googling.

推荐答案

如果你想在同一个视图控制器中有2个表视图,请执行以下操作:

If you want 2 tableviews in the same view controller do the following:


  1. 创建一个UIViewController作为委托

  1. Create a UIViewController that is a delegate to

UITableViewDelegate, UITableViewDataSource


  • 在故事板中为此视图控制器添加2个uitableviews

  • 在ViewController.h文件中为UITableViews添加2个IBOutlets,如下所示:

  • Add 2 uitableviews to this view controller in the storyboard
  • Add 2 IBOutlets for the UITableViews in your ViewController.h file like this:

    @property (nonatomic, weak) IBOutlet UITableView *tableView1;
    @property (nonatomic, weak) IBOutlet UITableView *tableView2;
    


  • 将IBOutlets连接到这些。

  • Connect the IBOutlets to these.

    在.m文件中添加tableview的委托函数,如下所示:

    Add the delegate functions for the tableview in the .m file somethng like this:

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
        // Return the number of rows in the section.
        if(tableView == self.tableView1){
            return whatever1;
        }
    
        if(tableView == self.tableView2){
            return whatever2;
        }
     }
    


  • 这篇关于将两个表视图添加到Xcode中的主视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆