当前位置: 首页 > 面试题库 >

在Swift中将数据源分离到另一个类

乌和畅
2023-03-14
问题内容

我正在尝试按照本文objc.io问题#1较浅的视图控制器中所述保持视图控制器的清洁。我在Objective-
C中测试了此方法,并且效果很好。我有一个单独的类,它实现UITableViewDataSource方法。

#import "TableDataSource.h"

@interface TableDataSource()

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSString *cellIdentifier;

@end

@implementation TableDataSource

- (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier {
    self = [super init];
    if (self) {
        self.items = items;
        self.cellIdentifier = cellIdentifier;
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = self.items[indexPath.row];

    return cell;
}

@end

从tableview控制器中,我要做的就是实例化此类的实例并将其设置为tableview的数据源,并且它可以完美地工作。

self.dataSource = [[TableDataSource alloc] initWithItems:@[@"One", @"Two", @"Three"] cellIdentifier:@"Cell"];
self.tableView.dataSource = self.dataSource;

现在我正在尝试在Swift中做同样的事情。首先,这是我的代码。它几乎是上述Objective-C代码的翻译。

import Foundation
import UIKit

public class TableDataSource: NSObject, UITableViewDataSource {

    var items: [AnyObject]
    var cellIdentifier: String

    init(items: [AnyObject]!, cellIdentifier: String!) {
        self.items = items
        self.cellIdentifier = cellIdentifier

        super.init()
    }

    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = items[indexPath.row] as? String

        return cell
    }

}

我这样称呼它。

let dataSource = TableDataSource(items: ["One", "Two", "Three"], cellIdentifier: "Cell")
tableView.dataSource = dataSource

但是该应用程序因以下错误而崩溃。

-[NSConcreteNotification tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例

我检查了和的init方法,TableDataSource各项和单元格标识符都通过了。我必须声明UITableViewDataSource方法public并删除override关键字,否则它将产生编译时错误。

我对这里发生的事情一无所知。有人可以帮我吗?

谢谢。


问题答案:

数据源创建一个属性,并将其与tableview一起使用。

class ViewController: UIViewController {

  @IBOutlet weak var tableView: UITableView!
  var dataSource:TableDataSource!

  override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = TableDataSource(items: ["One", "Two", "Three"], cellIdentifier: "Cell")

   tableView.dataSource = dataSource

  }
}


 类似资料:
  • 问题内容: 我不太明白为什么我不能将Int []从一个函数传递到另一个函数: 这给了我以下错误: 谢谢你的帮助! 问题答案: 中的参数称为 可变参数 。这意味着您可以传入该参数类型的可变数量,并且传入的所有内容都将转换为该类型的数组,以供您在函数中使用。 因此,内部的参数是一个数组,而不是所期望的一组参数。 您可能想要像这样重载以接受其中任何一个,因此您的平均函数可以调用适当的版本:

  • 在SQL Server中,如何将数据从一个表复制/追加到具有相同架构的另一个表中? 编辑: 假设有一个问题 它使用与表2中相同的模式和数据创建表1。 有没有这样的短查询只将整个数据复制到一个已经存在的表中?

  • 问题内容: 如何从一个数据库复制到另一个数据库。 数据库名称visco 我想将所有表从visco数据库复制到新数据库名称作为neptune 我创建了一个没有任何表的数据库,然后尝试从database1.bak文件还原数据库,然后显示错误为 需要查询帮助 问题答案: 使用SQL Server Management Studio: 选项1 ->右键单击要复制的数据库 ->选择“任务”>“生成脚本” -

  • 问题内容: 我想从一个表中读取所有数据,然后将一些数据插入到另一个表中。我的查询是 但我有一个错误 请帮我。 问题答案: 您可以使用INSERT … SELECT语法。请注意,您可以在SELECT部分​​中直接引用“ 1”。

  • 虽然Swift中的link:Add a element to a array展示了如何将一个项目添加到一个数组中,但我的问题是如何将一个数组作为一个项目添加到另一个数组中。 我没有发现任何类似的问题——我有以下阵列: 和 我想将第一个数组作为一个整体附加到另一个数组,所以我得到这样的结果: 请注意,这不是预期的结果: 我将如何实现这一点?

  • 问题内容: 我有2个不同的表,但各列的命名略有不同。我想从一个表中获取信息,然后将其放入另一个表中。仅当表1中的“信息字段”不为null时,才需要将表1中的信息放入表2中。表2在创建任何东西时都有一个唯一的ID,因此插入的任何东西都需要获得下一个可用的ID号。 表格1 表2 问题答案: 这应该工作。您无需担心Table2中的identify字段。