问题
How do I do it when if status is equal to static I don't want to show it in the table? Code is below. I only wanted to show all the data but not the data with status == "static".
Response [["status": {
name = static;
}, "sched": 2015-01-01T08:00:00+08:00, "desc": Brush your teeth, "id": 249, "reward": 1.00, "parent": , "child": , "date_created": 2018-04-25T14:27:20.405928+08:00, "name": aBrush your teeth, "occurrence": {
name = once;
}, "type": , "date_modified": 2018-04-26T11:56:02.030647+08:00], ["status": {
name = static;
}, "sched": 2015-01-01T08:00:00+08:00, "desc": Brush your teeth, "id": 250, "reward": 1.00, "parent": , "child": , "date_created": 2018-04-25T14:28:49.780354+08:00, "name": Brush your teeth, "occurrence": {
name = once;
}, "type": , "date_modified": 2018-04-26T11:56:05.616333+08:00], ["status": {
name = static;
}, "sched": 2015-01-01T08:00:00+08:00, "desc": Brush your teeth, "id": 252, "reward": 1.00, "parent": , "child": , "date_created": 2018-04-25T14:31:02.274405+08:00, "name": Brush your teeth, "occurrence": {
name = once;
}, "type": , "date_modified": 2018-04-26T11:59:57.676148+08:00], ["status": {
name = static;
}, "sched": 2015-01-01T08:00:00+08:00, "desc": Brush your teeth, "id": 253, "reward": 1.00, "parent": , "child": , "date_created": 2018-04-25T14:34:37.097498+08:00, "name": Brush your teeth, "occurrence": {
name = once;
}, "type": , "date_modified": 2018-04-26T09:42:24.633359+08:00], ["status": {
name = static;
}, "sched": 2015-01-01T08:00:00+08:00, "desc": Brush your teeths, "id": 254, "reward": 1.00, "parent": , "child": , "date_created": 2018-04-25T14:36:53.766088+08:00, "name": Brush your teeth, "occurrence": {
name = once;
}, "type": , "date_modified": 2018-04-26T11:56:15.757769+08:00], ["status": {
name = ongoing;
}, "sched": 2018-04-19T15:54:24.657644+08:00, "desc": {
"questions" : [
{
"b" : 2,
"a" : 1
},
{
"b" : 3,
"a" : 2
},
{
"b" : 2,
"a" : 8
},
{
"b" : 9,
"a" : 7
},
{
"b" : 3,
"a" : 6
}
],
"operation" : "+"
}, "id": 260, "reward": 1.00, "parent": shit, "child": , "date_created": 2018-04-26T10:13:42.913149+08:00, "name": chorename, "occurrence": {
name = once;
}, "type": homework, "date_modified": 2018-04-26T10:13:42.953485+08:00]]
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
print("All Result: " , getTempDetails)
if let str = getTempDetails["status"] as? [String: String] {
if let name = str["name"] {
if name == "ongoing" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = created
// cell.label.textColor = UIColor(red: 0.9294, green: 0.3333, blue: 0.1804, alpha: 1.0)
// cell.backgroundColor = created
}else if name == "approved" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = done
}
else if name == "for approval" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = pending
}else if name == "near expiry" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = neardue
} else if name == "expired" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = expired
}else if name == "static" {
cell.toDoItemLabel.text = getTempDetails["name"] as? String
cell.statuslabel.backgroundColor = UIColor(red: 0.7686, green: 0.4235, blue: 0.3725, alpha: 1.0)
//
} else {
print("false")
cell.toDoItemLabel.text = "lols"
}
}
}
}
回答1:
Firstly, you should not create a cell if you don't want to show it. If you are, you are making unnecessary extra calls.
If you are currently returning the total count of your datasource at numberOfRowsInSection, you must return the count of items with status != "static" instead. This way the cellForRowAt will be called the correct amount of times.
Then, you have 2 options:
- Have a separate datasource which doesn't contain items with
status == "static"and use it directly atcellForRowAt; - Filter, at
cellForRowAt, your current datasource, so that it returns only the correct items.
回答2:
Before passing data to tableview methods, you must have to remove entries from getTempDetails dictionary which you don't want.. means create same dictionary with removing data where name == "static"... and use newly created dictionary for table
来源:https://stackoverflow.com/questions/50033748/swift-ios-how-selected-data-to-a-table