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

最新更新