I'd like to find out all UITableViewCells (number varies) which have an detailTextLabel with the text "Done" in it. How can I do this in Swift?
You should not do this. Neither in Swift nor in any other language. You should query the dataSource because that is where the true state is stored. If you scroll a cell offscreen it get's reused, and the text gets set to something else. You can't rely on that information. Even worse, if you rely on texts in your cell you never get the whole picture about your dataSource, because the cells that are offscreen simply don't exist.
Here's what you should do: Somewhere in your code you set the text to "Done"
depending on the state of an object. Use the same decision to find all objects that are done.
For example, if your object has an isDone
getter, you hopefully use something like this to create the cell:
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
}
as you can see you decide depending on isDone
if the "Done"
text will be displayed.
So you can create a function that uses the same test to create an array that contains only projects that are done.
func projectsThatAreDone(allProjects: [Project]) -> [Project] {
let matchingProjects = allProjects.filter {
return $0.isDone
}
return matchingProjects
}
and you then use let doneProjects = projectsThatAreDone(allProjects)