将数据源分离到Swift中的另一个类 [英] Separating Data Source to another class in Swift

查看:120
本文介绍了将数据源分离到Swift中的另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保持视图控制器的清洁,如本文所述 objc.io Issue #1更轻的视图控制器。我在Objective-C中测试了这个方法,它工作正常。我有一个单独的类实现 UITableViewDataSource 方法。

I'm trying to keep my view controllers clean as described in this article objc.io Issue #1 Lighter View Controllers. I tested this method in Objective-C and it works fine. I have a separate class which implements UITableViewDataSource methods.

#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的数据源并且它完美地工作。

From the tableview controller, all I have to do is instantiate a instance of this class and set it as the tableview's data source and it works perfectly.

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

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

Now I'm trying to do the same in Swift. First here's my code. Its pretty much of a translation of the Objective-C code above.

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

但该应用程序崩溃时出现以下错误。

But the app crashes with the following error.

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

我检查了 TableDataSource init 方法以及项目和单元格标识符过得很好。我必须声明 UITableViewDataSource 方法 public 并删除覆盖关键字否则会产生编译时错误。

I checked the init method of TableDataSource and the items and the cell identifier gets passed fine. I had to declare the UITableViewDataSource methods public and remove the override keyword otherwise it would give compile time errors.

我对这里出了什么问题一无所知。有人可以帮帮我吗?

I'm clueless on what's going wrong here. Can anyone please help me out?

谢谢。

推荐答案

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

Create a property for data source and use it with your 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

  }
}

这篇关于将数据源分离到Swift中的另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆