繁体   English   中英

UITableview没有从超级视图中删除

[英]UITableview not removing from superview

我将使用单元格中的按钮删除UITableView中的单元格。

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

但是我的UITableView虽然计数为0,但并未删除。任何人都可以看到我做错了什么? 请帮我

如我所料,您在numberOfRows返回了arrayImageTable.count。

所以:

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

总是错的。 因为如果在numberOfRow返回arrayImageTable.count,则indexPath.row不能等于arrayImageTable.count。 尝试这个:

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)

希望对您有帮助。

暂无
暂无

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

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