ios, swift : multiple tables, 单 View Controller

标签 ios uitableview

我见过类似的问题,但我是 iOS 的新手,理解不够,无法将答案应用到我的场景中。我正在制作一个包含核心数据的 iPad 应用程序,并且想要一个并排显示两个 tableView 的横向 View 。我不知道如何从我的 vc.swift 文件中指定第二个 tableView。此代码显示两个相同的表。编辑:我现在可以指定不同的 TableView ,但我不能向每个 TableView 发送不同的核心数据。问题似乎是从 DidLoad 开始的,它看不到 tableView,所以每次都必须获取所有数据。

数据来自同一个实体,只是具有不同的属性(这就是为什么我从 playerFetchRequest 中创建了一个带有参数的函数 - 我想我可以使用 diff 参数发出不同的获取请求):

import UIKit
import CoreData

class CustomTableViewCell : UITableViewCell {
    @IBOutlet var l1: UILabel?
    @IBOutlet var l2: UILabel?

    func loadItem(#number: String, name: String) {
        l1!.text = number
        l2!.text = name
    }
}

class ViewController: UIViewController, UITableViewDelegate, NSFetchedResultsControllerDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    //this is my second table - Ive connected it in the IB to this VC
    @IBOutlet var tableView2: UITableView!

    let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
    var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()

    func playerFetchRequest(playerType: String) -> NSFetchRequest {
        let fetchRequest = NSFetchRequest(entityName: "Players")
        let sortDescriptor = NSSortDescriptor(key: "number", ascending: true)
        let filterForwards = NSPredicate(format: "%K = %@", "type", playerType)
        fetchRequest.sortDescriptors = [sortDescriptor]
        fetchRequest.predicate = filterForwards
        return fetchRequest
    }

    func getFetchedResultController(playerType: String) -> NSFetchedResultsController {
        fetchedResultController = NSFetchedResultsController(fetchRequest: playerFetchRequest("Forward"), managedObjectContext:managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
        return fetchedResultController
    }

    //remember: to create a table with multiple sections just implement the numberOfSectionsInTableView(_:) method
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects
        {return numberOfRowsInSection} else {return 0}
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
        let player = fetchedResultController.objectAtIndexPath(indexPath) as DataModel
        cell.l2?.text = player.lastName + ", " + player.firstName
        cell.l1?.text = player.number
        return cell
    }

    func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        println("You selected cell #\(indexPath.row)!")
    }

    override func viewDidLoad() {
        var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "customCell")
        fetchedResultController = getFetchedResultController("Forward")
        fetchedResultController.delegate = self
        fetchedResultController.performFetch(nil)
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func controllerDidChangeContent(controller: NSFetchedResultsController!) {
        tableView.reloadData()
    }
}

最佳答案

我认为你应该这样检查

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    {
 if ([tableView isEqual: tableView1]) {
// Do something
  }

  else { // tableView == tableView2
// Do something else
  }
}

与tableview的其他方法类似。

关于ios, swift : multiple tables, 单 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26455368/

相关文章:

ios - Collection View 单元格导航

ios - 自定义 UITableView 单元格错误

ios - Swift 3 将 JSON 数据排序/分组到带有部分的 TableView 中

ios - 从过滤数据崩溃的 TableView 中删除项目

ios - 在 NSDictionary 中获取内部键的键对象的最佳方法?

iOS Xcode Swift 自动完成功能坏了?

iphone - 有没有办法在不进入 iPhone 设置的情况下停止应用程序通知?

ios - 为什么我的 textView 没有隐藏在我的 CustomCell 类中

ios - 阻止 UITableView 接收两次触摸(嵌套弹出动画会导致导航栏损坏)

ios - 不同的颜色,即使 RGB 值相等