Swift UITableViewCell的内容
我有一个TableView和一个基于数组的单元格。现在我想根据单元格上显示的数组条目更改一个变量Swift UITableViewCell的内容,swift,uitableview,switch-statement,Swift,Uitableview,Switch Statement,我有一个TableView和一个基于数组的单元格。现在我想根据单元格上显示的数组条目更改一个变量 var array = ["first", "second", "third"] var result = String override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } o
var array = ["first", "second", "third"]
var result = String
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = self.tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell
Cell.textLabel?.text = array[indexPath.row]
// and here I need the content of the Cell
switch Cell."content argument" {
case "first":
var result = "works"
case "second":
var result = "works also"
case "third":
var result = "what a surprise, works also"
default:
var result = "doesn't work"
}
return Cell
}
这里我需要参数来得到单元格的内容。请不要用“你必须创建一个新的文件或函数或扩展名”,不要,请用参数 基于
单元格.textlab?.text
切换时遇到的问题是,textlab是可选的
,因此它的文本也是可选的
可选
是一个包含两个案例的枚举,。部分
和。无
,这就是案例不起作用的原因
有两种可能的解决方案
Cell.textLabel?.text = array[indexPath.row]
if let text = Cell.textLabel?.text {
switch text {
case "first":
var result = "works"
case "second":
var result = "works also"
case "third":
var result = "what a surprise, works also"
default:
var result = "doesn't work"
}
}
let text = array[indexPath.row]
Cell.textLabel?.text = text
switch text {
case "first":
var result = "works"
case "second":
var result = "works also"
case "third":
var result = "what a surprise, works also"
default:
var result = "doesn't work"
}
let text = array[indexPath.row]
let result: String
switch text {
case "first": result = "works"
case "second": result = "works also"
case "third": result = "what a surprise, works also"
default: result = "doesn't work"
}
Cell.textLabel?.text = text
Cell.detailTextLabel?.text = result
以下是所需的所有代码:
我相信您正在寻找开关电池。textLabel?.text您能详细说明您想要实现的目标吗?你指的是什么论点和内容?Swift中不支持您使用的对象“字符串”语法。另外,我建议不要使用大写变量名,但这是另一个主题。谢谢,但我以前尝试过,但没有用,它会给我错误代码“String”不能是“String”中的成员@PascalCzasny是否要为数组元素显示不同的UITableViewCell?不,我想更改基于witch cell的var结果,我单击了这三种方法,但它总是显示我不起作用。我不知道为什么它对你不起作用,你可能做了其他错误的事情,所以我添加了一个示例,包含所有需要的代码。我刚刚测试过,它工作得很好。它确实有用…我甚至上传了一个截图!不相信我?在这里,下载这个项目:好吧,现在它部分时间工作,因为在每个单元格上我都会得到“第三个”的结果,所以在每个单元格上都有惊喜的效果
import UIKit
class TableViewController: UITableViewController {
var array = ["first", "second", "third"]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let text = array[indexPath.row]
let result: String
switch text {
case "first": result = "works"
case "second": result = "works also"
case "third": result = "what a surprise, works also"
default: result = "doesn't work"
}
cell.textLabel?.text = text
cell.detailTextLabel?.text = result
return cell
}
}