繁体   English   中英

如何删除作为字典Swift的TableViewCell

[英]How to delete TableViewCell that being a Dictionary Swift

我正在尝试使用滑动删除来删除表格视图单元格,但是我无法使用在代码中标记的行,不确定原因。 我的单元格是由一个词典制作的,标题为键,详细信息为值。

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

你不能使用;

favDict.removeAtIndex(indexPath!.row)

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

favDict.removeValueForKey("yourkey")

编辑:

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

如果您仍在寻找Dictionary,则可以执行以下操作:

实现以下方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM