添加滑动以删除UITableViewCell [英] Add swipe to delete UITableViewCell

查看:126
本文介绍了添加滑动以删除UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITableView 创建一个CheckList应用程序。我想知道如何添加滑动来删除 UITableViewCell

I am making a CheckList application with a UITableView. I was wondering how to add a swipe to delete a UITableViewCell.

这是我的ViewController.swift:

This is my ViewController.swift:

import UIKit

class ViewController: UIViewController,  UITextFieldDelegate, UITableViewDelegate,      UITableViewDataSource {

var tableView: UITableView!
var textField: UITextField!
var tableViewData:Array<String> = []

// Define Colors

let lightColor: UIColor = UIColor(red: 0.996, green: 0.467, blue: 0.224, alpha: 1)
let medColor: UIColor = UIColor(red: 0.973, green: 0.388, blue: 0.173, alpha: 1)
let darkColor: UIColor = UIColor(red: 0.800, green: 0.263, blue: 0.106, alpha: 1)
let greenColor: UIColor = UIColor(red: 0.251, green: 0.831, blue: 0.494, alpha: 1)

init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    // Custom initialization
}

override func viewDidLoad() {
    super.viewDidLoad()

    //Set up table view

    self.tableView = UITableView(frame: CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100), style: UITableViewStyle.Plain)
    self.tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myCell")
    self.tableView.backgroundColor = darkColor
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
    self.tableView.delegate = self
    self.tableView.dataSource = self

    self.view.addSubview(self.tableView)

    //Set up text field

    self.textField = UITextField(frame: CGRectMake(0, 0, self.view.bounds.size.width, 100))
    self.textField.backgroundColor = lightColor
    self.textField.font = UIFont(name: "AvenirNext-Bold", size: 26)
    self.textField.delegate = self

    self.view.addSubview(self.textField)



}

//Table View Delegate

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

    return tableViewData.count

}

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

    var myNewCell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as MyTableViewCell
    myNewCell.text = self.tableViewData[indexPath.row]

    return myNewCell

}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    let mySelectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)

    //Colors

    mySelectedCell.detailTextLabel.textColor = UIColor.whiteColor()
    mySelectedCell.tintColor = UIColor.whiteColor()

    //Setup Details / Date

    let myDate:NSDate = NSDate()
    var myDateFormatter:NSDateFormatter = NSDateFormatter()
    myDateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

    mySelectedCell.detailTextLabel.text = myDateFormatter.stringFromDate(myDate)
    mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
    mySelectedCell.backgroundColor = greenColor

}

override func prefersStatusBarHidden() -> Bool {
    return true

}

//Text Field Delegate

func textFieldShouldReturn(textField: UITextField!) -> Bool {

    tableViewData.append(textField.text)
    textField.text = ""
    self.tableView.reloadData()
    textField.resignFirstResponder()
    return true

}

}

这是MyTableViewCell.swift:

And this is MyTableViewCell.swift:

import UIKit

class MyTableViewCell: UITableViewCell {

let medColor: UIColor = UIColor(red: 0.973, green: 0.388, blue: 0.173, alpha: 1)

init(style: UITableViewCellStyle, reuseIdentifier: String) {
    super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)

    self.textColor = UIColor.whiteColor()
    self.backgroundColor = medColor
    self.selectionStyle = UITableViewCellSelectionStyle.None
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

我使用iOS8作为部署目标(不确定它会产生什么差异)。

I am using iOS8 as deployment target (not sure of the difference it will make).

推荐答案

添加这两个函数:

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

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        // handle delete (by removing the data from your array and updating the tableview)
    }
}

Swift 3.0:

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.delete) {
        // handle delete (by removing the data from your array and updating the tableview)
    }
}

在3.0以上,这些需要覆盖

这篇关于添加滑动以删除UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆