2014-11-22 34 views
1

问题1:滚动时复选标记消失。将UITableView选择添加到阵列,滚动时消失的复选标记 - Swift

第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) 

} 

回答

0

问题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去除重复元素来自你的阵列。一种方法是使用一个集合,它不会保持顺序,但会确保元素只出现一次。

相关问题