我将两个Table View放在我的main.storyboard上。然后我为它们创建了两个新的.swift类文件:VideoTableViewController和LinkTableViewController。我如何链接这两个?看起来我可能做错了,因为我读过我需要将委托链接到表视图所在的View。我希望它链接到我为Table View创建的控制器。 我要做的是并排放置两个列表,一个带有视频链接,另一个带有网络链接。我已经有了数组中的数据。我只需要将数据放入正确的表视图的单元格中。
我注意到我可以将一个Table View Controller放到故事板上,但我不这么认为 这是我想要的,因为它是一个完全不同的观点。我希望这些看法是相同的。
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
}
}
}
}
}
那么,我是不是错了?显然我正在做的事情很奇怪,因为我无法通过谷歌搜索找到很多直接的信息。
答案 0 :(得分:5)
如果您想在同一视图控制器中执行2个表视图,请执行以下操作:
创建一个UIViewController,它是
的委托UITableViewDelegate, UITableViewDataSource
在ViewController.h文件中为UITableViews添加2个IBOutlet,如下所示:
@property (nonatomic, weak) IBOutlet UITableView *tableView1;
@property (nonatomic, weak) IBOutlet UITableView *tableView2;
将IBOutlets连接到这些。
在.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;
}
}
答案 1 :(得分:0)
@hackerinheels提出的解决方案对我来说有点不同:在视图控制器中使用UITableView就像单选按钮组一样。最初它没有用,因为我错过了在View Controller的viewDidLoad()中连接委托和数据源的步骤:
tableView.delegate = self
tableView.dataSource = self
添加此步骤后,它开始正常工作。
即使对于经验丰富的人来说,这也不是一件容易的事,但对于像我这样的外行来说,将其拼写可能会很有帮助。
对于一个View Controller中2个TableViews的解决方案,@ hackerinheels提出了5个步骤+上述对viewDidLoad()的更改,效果很好!