在 Xcode 中将两个 Table View 添加到主 View

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25019136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:18:09  来源:igfitidea点击:

Adding two Table Views to the main View in Xcode

iosxcodeuitableviewswift

提问by Burning Hippo

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.

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

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:

视频表视图控制器:

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

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

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.

那么,我这样做是错误的吗?显然这是我正在做的一件奇怪的事情,因为我在谷歌上找不到太多直接的信息。

回答by hackerinheels

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

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

  1. Create a UIViewController that is a delegate to

    UITableViewDelegate, UITableViewDataSource
    
  2. Add 2 uitableviews to this view controller in the storyboard
  3. 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;
    
  4. Connect the IBOutlets to these.

  5. 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;
        }
     }
    
  1. 创建一个 UIViewController,它是一个委托

    UITableViewDelegate, UITableViewDataSource
    
  2. 在故事板中向这个视图控制器添加 2 个 uitableviews
  3. 在 ViewController.h 文件中为 UITableViews 添加 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;
        }
     }