我想找出所有的UITableViewCells (数字变化的),其中有一个detailTextLabel,其中的文本“完成”。我怎么能在斯威夫特做这件事?
发布于 2014-11-28 15:45:30
你不应该这么做。无论是斯威夫特语还是其他语言。您应该查询dataSource,因为这是存储真实状态的地方。如果你在屏幕外滚动一个单元格,它就会被重用,文本会被设置为其他东西。你不能依赖那些信息。更糟糕的是,如果您依赖于单元格中的文本,您就永远得不到关于您的dataSource的全貌,因为屏幕外的单元格根本不存在。
下面是您应该做的事情:在代码中的某个地方,根据对象的状态将文本设置为"Done"。使用相同的决定查找所有已完成的对象。
例如,如果您的对象有一个isDone getter,您希望使用类似的方法来创建单元格:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProjectCell", forIndexPath: indexPath) as ProjectCell
let project = allProjects[indexPath.row]
if project.isDone {
cell.detailTextLabel?.text = "Done"
}
else {
cell.detailTextLabel?.text = nil
}
return cell
}如您所见,您将根据isDone来决定是否显示"Done"文本。
因此,您可以创建一个函数,该函数使用相同的测试来创建一个只包含已完成的项目的数组。
func projectsThatAreDone(allProjects: [Project]) -> [Project] {
let matchingProjects = allProjects.filter {
return $0.isDone
}
return matchingProjects
}然后使用let doneProjects = projectsThatAreDone(allProjects)
https://stackoverflow.com/questions/27191636
复制相似问题