带有协议问题的快速 UITableViewCell [英] swift UITableViewCell with protocol issue

查看:21
本文介绍了带有协议问题的快速 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个协议

protocol AttibuteValueCellProtocol {
    func set(attribute: String, value: String)
}

在我的 tableView 的数据源方法中,我希望我的单元格确认此协议.在objective-c中我可以这样做,但是在SWIFT中当我尝试使用 as?UITableViewCell.在objective-C中,如果我这样做,它总是有效 ((UITableViewCell *)cell)

And in my tableView's datasource method I want my cell to confirm to this protocol. In objective-c I could do that, but in SWIFT it gives the error when I try to use as? UITableViewCell<AttibuteValueCellProtocol>. In objective-C it always worked if I do ((UITableViewCell<AttibuteValueCellProtocol> *)cell)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? UITableViewCell<AttibuteValueCellProtocol>
}

我可以用什么代替它?

附言错误看起来像这样 http://take.ms/OHRA3

P.S. Error looks like this http://take.ms/OHRA3

我不需要扩展,因为当我 deque cell 时,它将是 CustomCell(并且 CustomCell 符合协议),但是这个类可以使用不同的单元标识符,所以这个类不应该知道关于单元格类名称,并不是每个单元格都符合 AttibuteValueCellProtocol ,这个类必须知道我们有符合协议 AttibuteValueCellProtocol 的单元格

I don't need extension , because when I deque cell , it will be CustomCell (and CustomCell conforms to Protocol), but this class can use different cell identifiers so this class shouldn't know about the cell class name and not every cell conforms to AttibuteValueCellProtocol , this class has to know that we have cell that conforms to protocol AttibuteValueCellProtocol

我的Objective-C我可以这样做并且它会编译(我知道它不会工作,因为view1不符合协议,但是创建正确的视图很容易并且它会工作,在这里我只是举个例子) :

I Objective-C I can do that and it will compile (I understand that it will not work because view1 doesn't conform to protocol, but it's easy to create proper view and it will work,here I just give an example) :

#import "TestViewController.h"

@interface TestViewController ()

@end

@protocol TestProtocol <NSObject>

- (void)testMethod;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIView *view1 = [[UIView alloc] init];
    UIView *viewForProtocolJustToShow = (UIView<TestProtocol> *)view1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

推荐答案

问题

正如错误所说,UITableViewCell 不是通用类型.使用这种语法,您会将符合协议设置通用对象的类型混淆.

As the error says, UITableViewCell is not a generic type. With this syntax you are confusing conforming to a protocol with setting the type of a generic object.

解决方案 1:扩展

要得到一个符合AttibuteValueCellProtocolUITableViewCell,你可以使用一个扩展:

To get a UITableViewCell that conforms to the AttibuteValueCellProtocol, you can use an extension:

extension UITableViewCell: AttibuteValueCellProtocol {
  // implementation of AttibuteValueCellProtocol requirements goes here
}

您必须实现 AttibuteValueCellProtocol 方法,因为 Swift(和 Objective-C)无法知道它们应该如何定义.

You will have to implement the AttibuteValueCellProtocol methods as Swift (and Objective-C) have no way of knowing how they should be defined.

解决方案 2:子类

如果您并不总是想要那些额外的方法,扩展整个 UITableViewCell 类可能会产生一些奇怪的副作用.你也可以子类化 UITableViewCell 并在那里实现 AttibuteValueCellProtocol 协议:

Extending the whole UITableViewCell class could create some strange side effects if you do not always want those extra methods. You can also just subclass UITableViewCell and implement the AttibuteValueCellProtocol protocol there:

class MyTableViewCell: UITableViewCell, AttibuteValueCellProtocol {
  // implementation of AttibuteValueCellProtocol requirements goes here
}

然后,你可以这样做:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
}

解决方案 3:协议

你也可以只检查返回的 UITableViewCell 是否符合特定的协议:

You can also just check if the returned UITableViewCell conforms to a specific protocol:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyProtocol
}

请注意,如果不符合,这会将单元格设置为 nil.

Note that this will set cell to nil if it does not conform.

这篇关于带有协议问题的快速 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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