首页 > 解决方案 > Swift iOS 如何选择数据到一个表

问题描述

如果 status 等于 static 我不想在表格中显示它,我该怎么做?代码如下。我只想显示所有数据,而不是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"
                    }
                }
                }
            }

标签: iosswiftselected

解决方案


首先,如果您不想显示单元格,则不应创建它。如果你是,你正在打不必要的额外电话。

如果您当前在 处返回数据源的总数numberOfRowsInSection,则必须status != "static"改为返回项目数。这样,cellForRowAt将被称为正确的次数。

然后,您有 2 个选项:

  1. 有一个单独的数据源,其中不包含带有的项目,status == "static"并直接在cellForRowAt;处使用它。
  2. 在 处过滤cellForRowAt您当前的数据源,使其仅返回正确的项目。

推荐阅读