【发布时间】: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