简体   繁体   中英

UITableview not removing from superview

I am going to delete cells in my UITableView using a button in the cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ATTACHMENTCELL", for: indexPath) as! AttachmentTableViewCell
    if let obj=dm.arrayImageTable[indexPath.row] as? [String:Any]
    {
        cell.lblTitle.text=obj["DocumentName"] as? String
        //cell.tag=obj["EmployeeCode"] as! Int
        cell.lblTitle.textColor=com.getfontColor()
        cell.imgvw.image=obj["Imag"] as? UIImage
        cell.btnDel.tag=indexPath.row
        cell.btnDel.addTarget(self, action: #selector(removeFromTable(sender:)), for: .touchUpInside)
    }

    return cell
}


func removeFromTable(sender:UIButton)
{

    var index:Int=sender.tag
    if(index>=dm.arrayImageTable.count)
    {
        index=0
        dm.arrayImageTable.remove(at: 0)
        dm.arrayAttachedDoc.remove(at: 0)
    }
    else
    {
        dm.arrayImageTable.remove(at: index)
        dm.arrayAttachedDoc.remove(at: index)
    }

    self.tblImage.deleteRows(at: [NSIndexPath.init(row: index, section: 0) as IndexPath], with: .left)

    var btnFrame=self.btnApplyLeave.frame
    btnFrame.origin.y=self.btnApplyLeave.frame.origin.y-30
   self.btnApplyLeave.frame=btnFrame
    if(dm.arrayImageTable.count<=0)
    {

        self.tblImage.removeFromSuperview()
    }
}

But my UITableView is not removing although the count is 0. Any one can see where is the wrong I have done? Please help me

As I'm guessing, at numberOfRows you've returned arrayImageTable.count.

So:

if(index>=dm.arrayImageTable.count)
    {
        index=0
        dm.arrayImageTable.remove(at: 0)
        dm.arrayAttachedDoc.remove(at: 0)
    }

always is wrong. Because indexPath.row can't equal arrayImageTable.count if you return arrayImageTable.count at numberOfRow . Try this:

var index = sender.tag == dm.arrayImageTable.count - 1 ? 0 : sender.tag
dm.arrayImageTable.remove(at: 0)
dm.arrayAttachedDoc.remove(at: 0)
self.tblImage.deleteRows(at: [IndexPath.init(row: index, section: 0)], with: .left)

Hope It will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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