ios - 将 UITableView 选择添加/删除到数组,滚动时复选标记消失 - Swift

标签 ios arrays uitableview swift

问题 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)

}

最佳答案

问题 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:在不太了解您希望保持唯一性的数据类型以及您如何以可能添加重复项的方式添加/删除数据的情况下,您可能需要查看 this answer用于从数组中删除重复的元素。一种方法是使用集合,它不会保持顺序但会确保元素只出现一次。

关于ios - 将 UITableView 选择添加/删除到数组,滚动时复选标记消失 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27079345/

相关文章:

ios - 单元格高度取决于标签文本

ios - 动画 layer.mask(不是形状层的 .path)

java - java中不同长度的数组的数组

c - 整数值如何使用 scanf() 传递给指针?

java - 修改数组中的标签

ios - 如何从 CoreDate 加载我的数据以在 tableView 中显示它?

ios - UITableViewCell 内的 UITextField

iphone - http GET 限制(iPhone)

iphone - 我在单例中使用保留还是复制?

iphone - [__NSCFNumber 长度] : unrecognized selector sent to instance 0x6d21350