gpt4 book ai didi

ios - 单击ui按钮时如何删除行

转载 作者:行者123 更新时间:2023-11-29 01:57:06 24 4
gpt4 key购买 nike

当我单击右侧的 UITabButtonItem 时,我想将 deleteButton 添加到我的 TableView 中,并在单击它时删除行。但我不知道该怎么做。每次我都会出错。

import UIKit

class CustomCell: UITableViewCell {

@IBOutlet weak var label: UILabel!

@IBOutlet weak var view: UIView!

var gesture: Bool!

override func awakeFromNib() {
super.awakeFromNib()

var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

leftSwipe.direction = .Left
rightSwipe.direction = .Right

self.addGestureRecognizer(leftSwipe)
self.addGestureRecognizer(rightSwipe)

gesture = false;


}


func handleSwipes(sender:UISwipeGestureRecognizer) {

if (gesture == false){

if (sender.direction == .Left) {

gesture = true;

var labelPosition = CGPointMake(self.view.frame.origin.x - 55.0, self.view.frame.origin.y);

UIView.animateWithDuration(0.5, animations: {

self.view.frame = CGRectMake( labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height)

}, completion: { (value: Bool) in

self.gesture = false;

})
}

if (sender.direction == .Right) {

gesture = true;

var viewPosition = CGPointMake(self.view.frame.origin.x + 55.0, self.view.frame.origin.y);

UIView.animateWithDuration(0.5, animations: {

self.view.frame = CGRectMake( viewPosition.x , viewPosition.y , self.view.frame.size.width, self.view.frame.size.height)

}, completion: { (value: Bool) in

self.gesture = false;

})
}

}
}

func buttonClick(sender : UIButton) {

let deleteButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let image = UIImage(named: "delete") as UIImage?
deleteButton.frame = CGRectMake(0, 0, 51, 46)
deleteButton.setImage(image, forState: .Normal)
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(deleteButton)

}


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


}

func setCell(labeltext :String)
{
self.label.text = labeltext
}

}

这是我的 View Controller

class controller: UIViewController, UITableViewDelegate,  UITableViewDataSource {

var arrayOfList: [List] = [List]()


override func viewDidLoad() {
super.viewDidLoad()


var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action: "")

editButton.tintColor = UIColor.whiteColor()

var settingsButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "settings"), style: UIBarButtonItemStyle.Plain, target: self, action : "")

settingsButton.tintColor = UIColor.whiteColor()

self.navigationItem.leftBarButtonItem = settingsButton

self.navigationItem.rightBarButtonItem = editButton


self.setUpList()
}


func setUpList()

{
var list1 = List(name:"Apple")
var list2 = List(name:"Banana")
var list3 = List(name:"Orange")
var list4 = List(name:"Pineapple")
var list5 = List(name:"Tomato")
arrayOfList.append(list1)
arrayOfList.append(list2)
arrayOfList.append(list3)
arrayOfList.append(list4)
arrayOfList.append(list5)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfList.count
}


func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

let list = arrayOfList[indexPath.row]
cell.setCell(list.name)

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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

最佳答案

有一个你必须使用的功能,使用这段代码你将拥有滑动功能来删除行:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}

在这部分代码之后:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOfList.count
}

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

let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

let list = arrayOfList[indexPath.row]
cell.setCell(list.name)

return cell
}

你应该添加这个函数:

func editClicked(sender: AnyObject?) {
if (self.tableView.editing) {
let editbutton = UIBarButtonItem(image: UIImage(named: "trash"), style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("editClicked:"))
self.navigationItem.rightBarButtonItem = editbutton
self.tableView.setEditing(false, animated: true)
} else {
let editbutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("editClicked:"))
self.navigationItem.rightBarButtonItem = editbutton
self.tableView.setEditing(true, animated: true)
}
}

像这样在你的 navigationBarButton 上调用它:

var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editClicked:"))

关于ios - 单击ui按钮时如何删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com