Ios 我在下面的代码中做了什么导致我出错?
我遇到以下错误: 1) 用于检查选项的“UITableViewCell”类型的非可选表达式 2) “UITableViewCell”类型的值没有成员“congigureCell” 请 1) 这个!结尾处标记Ios 我在下面的代码中做了什么导致我出错?,ios,swift,compiler-errors,Ios,Swift,Compiler Errors,我遇到以下错误: 1) 用于检查选项的“UITableViewCell”类型的非可选表达式 2) “UITableViewCell”类型的值没有成员“congigureCell” 请 1) 这个!结尾处标记 countryList.dequeueReusableCell(withIdentifier: "cell")! 使用“强制展开”将其设置为非可选,所以如果让它执行,则不应在内部检查它,或者更好的方法是删除它!标记 2) congigureCell可能是不同类的方法,而不是UITableV
countryList.dequeueReusableCell(withIdentifier: "cell")!
使用“强制展开”将其设置为非可选,所以如果让它执行,则不应在内部检查它,或者更好的方法是删除它!标记
2) congigureCell可能是不同类的方法,而不是UITableViewCell。您应该使用该类替换UITableViewCell,以对其进行强制转换
if let cell:UITableViewCell = countryList.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell
让myCell=单元格为!迈塞尔
确保您已完成以下步骤
YourTableview
的委托和数据源分配给YOURViewController.swift
李>
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 }