【问题标题】:Add/Remove UITableView Selection to Array, Checkmarks Disappear upon scroll - Swift添加/删除 UITableView 选择到数组,复选标记在滚动时消失 - Swift
【发布时间】:2014-11-22 15:59:10
【问题描述】:

问题 1:滚动时复选标记不断消失。

问题 2:需要帮助添加/删除具有唯一 ID 的数组以防止重复。

我正在尝试从空数组中插入/删除 cellTextLabel。我似乎找不到一个好的解决方案。以下是我尝试过的方法以及失败的原因。

错误的选项 1

// Error = Out of range (I understand why)
myArray.insert(cell.textLabel.text, atIndex: indexPath)

错误的选项 2

// I won't have a way to reference the array item afterwards when I need to remove it. Also, this option allows for the same string to be entered into the array multiple times, which is not good for me.
myArray.insert(cell.textLabel.text, atIndex: 0)

以下是到目前为止的代码,非常感谢任何帮助。谢谢。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let row = indexPath.row
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("items", forIndexPath: indexPath) as UITableViewCell

    var myRowKey = myArray[row]
    cell.textLabel.text = myRowKey

    cell.accessoryType = UITableViewCellAccessoryType.None
    cell.selectionStyle = UITableViewCellSelectionStyle.None


    return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {


    let selectedCell  = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

    if selectedCell.accessoryType == UITableViewCellAccessoryType.None {

    selectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
    }

    var selectedItem = selectedCell.textLabel.text!

    println(selectedItem)

}

func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {

    let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

    if deSelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark {
        deSelectedCell.accessoryType = UITableViewCellAccessoryType.None
    }

    var deSelectedItem = deSelectedCell.textLabel.text!

    println(deSelectedItem)

}

【问题讨论】:

    标签: ios arrays uitableview swift


    【解决方案1】:

    问题 1:由于didDeselectRowAtIndexPath 方法中的以下行,当您滚动时,您的复选标记不断消失:

    let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
    

    cellForRowAtIndexPath 的调用将创建一个新单元格。它不会修改 UITableView 屏幕上当前可见的单元格。这是因为当项目在屏幕上滚动和滚动时,单元格会被重复使用,并会加载新数据。

    要保留单元格的选择状态,您需要稍微升级一下数据模型。现在您的数据来自myArray,它是一个String 数组。您可以尝试以下方法:

    struct Item {
      var name: String // The string value you are putting in your cell
      var isSelected: Bool // The selected status of the cell
    }
    

    然后你会像这样定义你的数据:

    var myArray = [
        Item(name: "Cell 1 value", isSelected: false),
        Item(name: "Cell 2 value", isSelected: false),
        ...
      ]
    

    你的tableView:didSelectRowAtIndexPath 方法看起来更像这样:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         // Toggle the selected state of this cell (e.g. if it was selected, it will be deselected)
        items[indexPath.row].isSelected = !items[indexPath.row].isSelected
    
         // Tell the table to reload the cells that have changed
        tableView.beginUpdates()
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
        tableView.endUpdates()
    
        // Reloading that cell calls tableView:numberOfRowsInSection and refreshes that row within the tableView using the altered data model (myArray array)
      }
    

    下次您点击该行时,tableView:didSelectRowAtIndexPath 方法将再次触发并切换该单元格的选定状态。告诉该单元格重新加载将刷新屏幕上实际可见的单元格。

    问题 2:在不太了解要保持唯一性的数据类型以及如何以可能添加重复项的方式添加/删除的情况下,您可能想看看 @ 987654321@ 用于从数组中删除重复元素。一种方法是使用集合,它不会保持顺序,但会确保元素只出现一次。

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多