ios - 如何在 Delegate 中检查 UITableView 中的当前 Controller

标签 ios swift uitableview

<分区>

我正在尝试学习一些 Swift 语言,我想知道如何将以下 Objective-C 转换为 Swift:

- (void)refreshDataOnChange {
    if (![tblSkillMaterials.delegate isKindOfClass:[self class]]) {
        tblSkillMaterials.delegate = self;
    }
    if (![tblSkillMaterials.dataSource isKindOfClass:[self class]]) {
        tblSkillMaterials.dataSource = self;
    }
    //Other Code Logic Here

}

更具体地说,我需要知道如何在新语法中使用 isKindOfClass。

func refreshDataOnChange() {
    if tblSkillMaterials.delegate?.isKind(of: ?) {
        tblSkillMaterials.delegate = self
    }
    if tblSkillMaterials.dataSource?.isKind(of: ?) {
        tblSkillMaterials.dataSource = self
    }
    //Other Code Logic Here
}

获取错误如:

Cannot convert value of type 'ViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type')

最佳答案

试试下面的代码,它对我来说工作正常:

func refreshDataOnChange() {
   if tblSkillMaterials.delegate?.isKind(of: <your view controller name>.self) {
     tblSkillMaterials.delegate = self
   }
   if tblSkillMaterials.dataSource?.isKind(of: <your view controller name>.self) {
     tblSkillMaterials.dataSource = self
   }
   //Other Code Logic Here
 }

关于ios - 如何在 Delegate 中检查 UITableView 中的当前 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588897/

相关文章:

ios - 类型 '[AnyHashable : Any]?' 没有下标成员 - 使用 Moltin API

ios - UISearchController UITableView 旋转显示

ios - 如何将 sqlite 数据库中的图像显示到 UIImageView

ios - 添加按钮到列表单元格

ios - 如何在 irate for ios 中编辑标题和消息?

ios - Swift:Class 不能声明为 public 因为它的 Super 类是内部的

ios - 将自定义单元格标签和 TextField 文本附加到字典数组

ios - 在单个 UIViewController 中交换 UITableViews

ios - Objective-C : Switch UITableViewCell when deselecting Cell

ios - 将正数转换为负数 - iOS