【问题标题】:How to filter an array which has different elements?如何过滤具有不同元素的数组?
【发布时间】:2015-12-27 19:36:35
【问题描述】:

我有一个 TableView,从中我得到一个数组中选定的 tableViewCell's 值,但它不是所需的数组:

[<NSIndexPath: 0xc000000000200016\> {length = 2, path = 0 - 1}: "test1234", <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}: "Judson"]

如何过滤得到["test1234","Judson"]

这是我正在使用的代码:

var selectedTextLabels = [NSIndexPath: String]()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! UsersTableViewCell
    if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
        cell.accessoryType = UITableViewCellAccessoryType.None;
        selectedTextLabels[indexPath] = nil
    } else {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark;
        if (cell.accessoryType == UITableViewCellAccessoryType.Checkmark){
            if let text = cell.collegeNameLbl?.text {
                selectedTextLabels[indexPath] = text
            }
        }
    }
    print("\(selectedTextLabels)  outside")
}

【问题讨论】:

    标签: ios arrays swift uitableview


    【解决方案1】:

    由于var selectedTextLabels = [NSIndexPath: String]() 是一个字典,您正在尝试检索其中的String 部分,即values,您可以通过以下方式获取它们:

    let values = Array(selectedTextLabels.values)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2018-11-05
      • 1970-01-01
      • 2012-09-17
      • 2015-12-10
      相关资源
      最近更新 更多