Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我在下面的代码中做了什么导致我出错?_Ios_Swift_Compiler Errors - Fatal编程技术网

Ios 我在下面的代码中做了什么导致我出错?

Ios 我在下面的代码中做了什么导致我出错?,ios,swift,compiler-errors,Ios,Swift,Compiler Errors,我遇到以下错误: 1) 用于检查选项的“UITableViewCell”类型的非可选表达式 2) “UITableViewCell”类型的值没有成员“congigureCell” 请 1) 这个!结尾处标记 countryList.dequeueReusableCell(withIdentifier: "cell")! 使用“强制展开”将其设置为非可选,所以如果让它执行,则不应在内部检查它,或者更好的方法是删除它!标记 2) congigureCell可能是不同类的方法,而不是UITableV

我遇到以下错误:

1) 用于检查选项的“UITableViewCell”类型的非可选表达式

2) “UITableViewCell”类型的值没有成员“congigureCell” 请

1) 这个!结尾处标记

countryList.dequeueReusableCell(withIdentifier: "cell")!
使用“强制展开”将其设置为非可选,所以如果让它执行,则不应在内部检查它,或者更好的方法是删除它!标记

2) congigureCell可能是不同类的方法,而不是UITableViewCell。您应该使用该类替换UITableViewCell,以对其进行强制转换

  • 这个!标记用于强制展开可以为零的可选值。但是“if let”和“guard let”已经检查了可选项,所以您不需要!马克
  • 只用

    if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell
    
  • 此行中的单元格为“UITableViewCell”,但congigureCell不是UITableViewCell的成员。 如果要使用自己的单元格(如MyCell),应将其转换为MyCell

  • 让myCell=单元格为!迈塞尔

    确保您已完成以下步骤

  • 将情节提要中的单元标识符添加到自定义单元。i、 e“细胞”
  • 通过故事板或代码将
    YourTableview
    的委托和数据源分配给
    YOURViewController.swift
  • 在YOURViewController.swift中,使用表的数据源访问单元格 视为
  • 添加子类UITableViewCell的自定义类并将其分配给 故事板中的漫游单元

    func tableView(tableView:UITableView,cellForRowAt indexPath:
    indexPath)->UITableView单元格{

    if let cell = countryList.dequeueReusableCell(withIdentifier: "cell") as! YOURTableViewCellClass  {
    
        let text: String!
    
        if inSearchMode {
    
            text = filteredCountriesList[indexPath.row]
    
        } else {
    
            text = countriesList[indexPath.row]
        }
    
        cell.congigureCell(text: text) // Error 2 happens here
    
        return cell }
    

  • 1.使用dequeueReusableCellWithIdentifier代替dequeueReusableCellWithIdentifier:forIndexPath:后者从不提供nil值,因此您不必担心“nil错误”

    2.UItableViewCell没有像您的情况那样配置单元格或congigureCell,您必须创建一个自定义tableViewCell,并将函数添加为configureCell(),然后在此行中

    如果let cell:UITableViewCell=countryList.dequeueReusableCell(带有标识符:“cell”)!作为UITableViewCell


    替换为UITableViewCell替换为作为您的CustomTableViewCellClass
    configureCell
    可能吗?如果任何答案解决了您的问题,请接受该答案,以便以后的用户可以从中受益。我如何完成#2?我找到了解决#1的方法。您的configureCell可能是在某个类中定义的。更改UITableV将代码中的iewCell添加到此类
    if let cell = countryList.dequeueReusableCell(withIdentifier: "cell") as! YOURTableViewCellClass  {
    
        let text: String!
    
        if inSearchMode {
    
            text = filteredCountriesList[indexPath.row]
    
        } else {
    
            text = countriesList[indexPath.row]
        }
    
        cell.congigureCell(text: text) // Error 2 happens here
    
        return cell }