从swift中的tableview中删除自定义单元格

时间:2015-03-11 05:43:10

标签: swift

我正在实现从tableview中删除自定义单元格的功能。

func DeleteData(sender : UIButton)
{
    var dbPath: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    dbPath = dbPath .stringByAppendingPathComponent("BillInfo.sqlite")
    var db = IMDDB()
    db.initWithPath(dbPath);
    var table : UITableView = view.viewWithTag(900) as UITableView
    var btnPosition :  CGPoint = sender.convertPoint(CGPointZero, toView: table)
    var index : NSIndexPath = table.indexPathForRowAtPoint(btnPosition)!
    var customCell = CustomeTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"Cell")
    var name : NSString =  customCell.lblName.text!//will give unexpectedly found nil error
   // println("\(customCell.lblName.text!)")
}


如何从自定义单元格访问所有控制器。
我有customcell.swift类。

override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
    super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    imgBill.backgroundColor = UIColor.blackColor()
    btnDelete.setBackgroundImage(UIImage(named: "01-gradient.png"), forState: UIControlState.Normal)
    btnDelete.setTitle(NSLocalizedString("BTN_DELETE", comment: ""), forState: UIControlState.Normal)
    btnDelete.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

    imgBill.setTranslatesAutoresizingMaskIntoConstraints(false)
    lblName.setTranslatesAutoresizingMaskIntoConstraints(false)
    lblType.setTranslatesAutoresizingMaskIntoConstraints(false)
    lblAmount.setTranslatesAutoresizingMaskIntoConstraints(false)
    btnDelete.setTranslatesAutoresizingMaskIntoConstraints(false)

    contentView.addSubview(imgBill)
    contentView.addSubview(lblName)
    contentView.addSubview(lblType)
    contentView.addSubview(lblAmount)
    contentView.addSubview(btnDelete)
     // i have already added the constraint on it.

1 个答案:

答案 0 :(得分:0)

func DeleteData(sender : UIButton)
{
    var table : UITableView = view.viewWithTag(900) as UITableView
    var btnPosition :  CGPoint = sender.convertPoint(CGPointZero, toView: table)
    var index : NSIndexPath = table.indexPathForRowAtPoint(btnPosition)!
    var cell = table.cellForRowAtIndexPath(index) as CustomeTableViewCell
    var name : String = cell.lblName.text!
    var dbPath: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    dbPath = dbPath .stringByAppendingPathComponent("BillInfo.sqlite")
    var db = IMDDB()
    db.initWithPath(dbPath);
    var arrayData : NSArray = db.lookupAllForSQL("select * from BillInfo")
    db.executeQuery("DELETE from BillInfo where Name = '\(name)' ", withError: nil)
    var arrayDeleteData : NSArray = db.lookupAllForSQL("select * from BillInfo")
    if arrayDeleteData.count < arrayData.count
    {
        var alert: UIAlertView = UIAlertView()
        alert.title = "Delete Bill Information"
        alert.message = "Delele Bill Information SucessFully"
        alert.delegate = self
        alert.addButtonWithTitle("Ok")
        alert.show()
    }
}