【问题标题】:How to delete TableViewCell that being a Dictionary Swift如何删除作为字典 Swift 的 TableViewCell
【发布时间】:2015-01-02 23:03:59
【问题描述】:

我正在尝试使用滑动删除来删除表格视图单元格,但我无法使用我在代码中标记的行,不知道为什么。我的单元格是由字典制作的,其中标题为键,详细信息为值。

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

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

    //Create the cell by the dictionary favDict
    for (key, value) in favDict{
        cell.textLabel?.text = key
        cell.detailTextLabel?.text = value
    }

    return cell
}

func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        favDict.removeAtIndex(indexPath!.row)// Line that is giving the error says Int is not convertible for [String: String]
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

分辨率:

import UIKit

class ViewController2: UIViewController, UITableViewDelegate {

    var dictKeys : [String]?

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        dictKeys = Array(favDict.keys)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dictKeys = Array(favDict.keys)
        return rowsInSec
    }



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


        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        var key = dictKeys?[indexPath.row]
        var data = favDict[key!]
        cell.textLabel?.text = key
        cell.detailTextLabel?.text = data

         return cell
    }


    func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
        return true
    }

    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
        if editingStyle == UITableViewCellEditingStyle.Delete
        {
            var key  = dictKeys?[indexPath.row]
            favDict.removeValueForKey(key!)
            dictKeys?.removeAtIndex(indexPath.row)
            rowsInSec--
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
        }
    }

【问题讨论】:

    标签: ios xcode uitableview swift dictionary


    【解决方案1】:

    你不能用;

    favDict.removeAtIndex(indexPath!.row)
    

    removeAtIndexNSMutableArray 的实例方法。要从字典中删除值,您可以使用:

    favDict.removeValueForKey("yourkey")
    

    编辑:

    NSMutableArray 相比,使用Dictionary 删除和加载数据有点困难。您还可以在 cellForRowAtIndexPath: 中放置一个循环。它只会将最后一个对象加载到每个单元格。

    如果你还在寻找字典,你可以这样做:

    实现如下方法:

    var dictKeys : [Strings]?
    override func viewDidLoad()
    {
        super.viewDidLoad()
        dictKeys = Array(favDict.keys)
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
    
       let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
       var key  = dictKeys?[indexPath.row]
       var data = favDict[key!]
       cell.textLabel?.text = key
       cell.detailTextLabel?.text = data
       return cell
    }
    
    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)
    {
        if editingStyle == UITableViewCellEditingStyle.Delete
        {
            var key  = dictKeys?[indexPath.row]
            favDict.removeValueForKey(key!)
            dictKeys?.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
    

    【讨论】:

    • 嗯,但这会删除密钥吗?如果我不知道要删除哪个单元格,我怎么知道要使用哪个键?
    • @Skal:它也会删除密钥。问题是你不能轻易地从 NSDictionary 中实现预期的行为。您需要使用复杂的机制来查找 yourKey。如果您仍然坚持保留字典,那么我会解释如何。
    • 我真的需要字典,否则我将无法实现我想要的。如果您能解释一下如何操作,我将不胜感激。
    • 有些东西不工作,Xcode 不允许我创建“var dictKeys?= nil”。只是一个广告,在你的 cellForRowAtIndexPath 函数中有一个额外的花括号
    • @Skal:那是我的错误,使用var dictKeys : [Strings]?
    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2023-03-18
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多