将两个表视图添加到Xcode中的主视图

我把两个表视图放到我的main.storyboard上。 然后,我为他们创build了两个新的.swift类文件:VideoTableViewController和LinkTableViewController。 我如何链接这两个? 它看起来像我可能做了这个错误,因为我读我需要委托链接到视图的表视图。 我希望它链接到我为表视图创build的控制器。 我想要做的是有两个并列的列表,一个是video链接,另一个是网页链接。 我已经在数组中的数据。 我只需要将这些数据放到适当的表格视图的单元格中。

我注意到我可以把一个表格视图控制器放到故事板上,但我不认为这是我想要的,因为这是一个完全不同的观点。 我希望这些都是一致的看法。

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:

这填充对象的数组,然后将数组传递到适当的表控制器。

 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 } } } } } 

那么,我该怎么做呢? 显然,这是一个奇怪的事情,因为我找不到很简单的信息,谷歌search。

如果您想在同一个视图控制器中执行以下两项操作:

  1. 创build一个委托给的UIViewController

     UITableViewDelegate, UITableViewDataSource 
  2. 在故事板中将2个可用视图添加到此视图控制器
  3. 为您的ViewController.h文件中的UITableView添加2个IBOutlets,如下所示:

     @property (nonatomic, weak) IBOutlet UITableView *tableView1; @property (nonatomic, weak) IBOutlet UITableView *tableView2; 
  4. 将IBOutlets连接到这些。

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

      - (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; } }