【问题标题】:Swift - iOS - Create a Newsfeed TableView from Multiple Data Sources/Other TableViewController DataSwift - iOS - 从多个数据源/其他 TableViewController 数据创建新闻源 TableView
【发布时间】:2018-04-03 19:47:24
【问题描述】:

我想创建一个新闻源/时间线样式的 TableView,它可以提取用户在我的应用中的各种 TableView 控制器中创建的其他条目。

当前功能:

这是应用程序的当前功能:用户导航到不同的类别并在 TableViews 中添加列表项:TableView Controller Set Up.

需要的功能:

我想做的是创建一个新的 TableViewController,它根据时间戳的顺序聚合所有这些条目。显示条目的 Timeline TableViewController 应该拉入用户之前已经输入到 TableViewCell 中的条目标题:Newsfeed/Timeline TableViewController.

代码:

我可以将应用程序的一个类别(也称为另一个 TableViewController)提供给我的新闻源/时间线视图,但不能提供多个来源。我认为最后一段代码是我唯一需要更改的地方......代码应该以某种方式有条件地确定条目来自哪个视图控制器,并将适当的标题分配给 cell.titleLabel.text。这可能吗?

为时间轴/新闻源设置 TableView 数据源的代码:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return oranges.count + blueberries.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "TimelineTableViewCell"
    
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TimelineTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TimelineTableViewCell.")
        
    }
    
    // Fetches the appropriate fruit for the data source layout.
    let orange = oranges[indexPath.row]
    cell.titleLabel.text = orange.orangeName
    let blueberries = blueberries[indexPath.row]
    cell.titleLabel.text = blueberries.blueberries.name
    return cell

实现继承后更新代码 (4.11.18) - 表视图控制器出现空白 : 表视图控制器:

import UIKit
import os.log

class TimelineTableViewController: UITableViewController {

//MARK: Properties

var fruits = [Fruit]()
var fruitList = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    
}

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

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruits.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "TimelineTableViewCell"
    
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TimelineTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TimelineTableViewCell.")
    }
    
    
    // Fetches the appropriate entry for the data source layout.
    
    let fruit = fruits[indexPath.row]
    cell.titleLabel.text = fruit.name
    return cell
    

 }
}

橙色类:

import UIKit
import os.log

class Orange: Fruit {

//MARK: Properties

override var name: String {
        set {
            super.name = ""
        }
        get {
            return super.name
        }
    }

var size: String
var color: String

//MARK: Archiving Paths

static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("fruits")

//MARK: Types
struct PropertyKey {
    static let size = "size"
    static let color = "color"
    let name = "name"
}

//MARK: Initialization
init? (name: String, size: String, color: String) {
    
    
    // Initialize stored properties.
    self.size = size
    self.color = color

    super.init(name: name)

}
//MARK: NSCoding
override func encode(with aCoder: NSCoder) {
    aCoder.encode(size, forKey: PropertyKey.size)
    aCoder.encode(color, forKey: PropertyKey.color)
    aCoder.encode(name, forKey: PropertyKey.name)
}

required convenience init?(coder aDecoder: NSCoder) {
    
    // The name is required. If we cannot decode a date string, the initializer should fail.
    guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
        os_log("Unable to decode the name for object.", log: OSLog.default, type: .debug)
        return nil
    }
    
    // The size is required. If we cannot decode a time string, the initializer should fail.
    guard let size = aDecoder.decodeObject(forKey: PropertyKey.size) as? String else {
        os_log("Unable to decode the size for object.", log: OSLog.default, type: .debug)
        return nil
        
    }
    
    // The color is required. If we cannot decode a name string, the initializer should fail.
    guard let color = aDecoder.decodeObject(forKey: PropertyKey.color) as? String else {
        os_log("Unable to decode the color for object.", log: OSLog.default, type: .debug)
        return nil
    }
    
    
    // Must call designated initializer.
    self.init(name: name, size: size, color: color)
    
 }
}

【问题讨论】:

  • 改为创建一个数组:fruit 数组,按照您想要的顺序组合橙子和蓝莓。然后,您可以检查索引indexPath.row(橙色或蓝莓)处的对象的类。您还可以做的是使用他们将遵循并使用它的协议,或者使它们继承自同一个类。
  • 感谢您的评论!您能否分享一些示例代码来展示您的建议?

标签: ios swift uitableview


【解决方案1】:

橙子和蓝莓应该继承自 Fruit 类,模型将是一个水果数组。你的代码可以是这样的:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruit.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "TimelineTableViewCell"

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TimelineTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TimelineTableViewCell.")
    }
    // Fetches the appropriate fruit for the data source layout.
    let fruit = fruits[indexPath.row]
    cell.titleLabel.text = fruits.name
    return cell
}

要设置水果 - 橙子和蓝莓,您可以使用继承或多态性。

要使用继承,如下所示:

class Fruit {
  var name: String {
    get {
      return "Fruit"
    }
  }
}

class Orange: Fruit {
  override var name: String {
    get {
      return "Orange"
    }
  }
}

class BlueBerry: Fruit {
  override var name: String {
    get {
      return "BlueBerry"
    }
  }
}

调试提示

你更新代码后,说table view是空的,找不到问题所在。以下是我可以为您提供的提示列表,这些提示可能有助于您找到问题。

  1. tableView:numberOfRowsInSection加个断点,看看有没有断。
  2. 如果坏了,看水果数组,数组的大小是0吗?

【讨论】:

  • 非常感谢您的帮助!你能分享一下 Table View Controller 和 Fruit 类的完整代码吗?我试图实施您的建议,但我的表视图控制器出现空白。
  • @swifty1990 你能告诉我你的代码,以便我知道为什么你的表格视图控制器出现空白吗?
  • 是的,当然!我已将 Table View Controller 和 Orange 类的代码添加到我原来的问题中。让我知道是否还有其他我可以添加的对您有帮助的...
  • @swifty1990 更新了答案,提供了一些可能帮助您找到问题的提示。
猜你喜欢
  • 2017-04-05
  • 1970-01-01
  • 2019-05-30
  • 2013-07-28
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多