动态中的Swift自定义滑动表视图单元格问题

时间:2016-07-11 05:37:33

标签: ios swift uitableview

我有一份评论列表。如果是我的用户自己的注释,当用户从左向右滑动时,将启用删除选项。删除注释后,它将重新加载表。用户可以在同一页面上添加新评论,同时也会重新加载表格。

我正在使用this repository and using this

问题在于;

它正在运作,但几乎没有错误。让我们解释一下这个案子。如果不是我的评论,当我滑动时也显示删除选项,因为我添加了新评论并删除了一些评论。在这种情况下,以前添加的滑动单元格不会被删除。我认为就是这种情况。无法实现它。

当我滚动评论时,它以错误的方式显示删除选项。我已通过滑动检查了用户ID。请指导我。它以正常方式工作。现在的要求是显示删除图标。

这是我的代码:

 class CustomCommentCellTableViewCell: PKSwipeTableViewCell {

@IBOutlet weak var timelineLabel  : UILabel!
@IBOutlet weak var commentMsg     : UILabel!
@IBOutlet weak var commenterName  : UILabel!
@IBOutlet weak var profName       : UILabel!
@IBOutlet weak var proIcon        : UIImageView!
var commentCount : Int?;
var userID : String?

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

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}


func configureCell(row:Int, userID: String,msg: String) {
    //  self.lblTitle.text = strToSet
    self.userID = userID;
    self.commentCount = row;
    print("row ==== :\(row) =====:\(msg)")
    print(":\(userID) ==loged=:\(Globals.sharedInstance.userId) =====:\(userID == Globals.sharedInstance.userId)")

    if(userID == Globals.sharedInstance.userId){
        self.addRightViewInCell()
    }else{
        self.resetCellState()
    }


}

func addRightViewInCell() {
    //Create a view that will display when user swipe the cell in right
    let viewCall = UIView()

    viewCall.backgroundColor = UIColor(red: 254.0/255.0, green: 53.0 / 255.0, blue: 63.0 / 255.0, alpha: 1.0)
    viewCall.frame = CGRectMake(0, 0,CGRectGetHeight(self.frame)+20,CGRectGetHeight(self.frame))
    //Add a label to display the call text

    //Add a button to perform the action when user will tap on call and add a image to display
    let btnCall = UIButton(type: UIButtonType.Custom)
    btnCall.frame = CGRectMake((viewCall.frame.size.width - 40)/2,viewCall.frame.size.height/2,40,40)
    btnCall.setImage(UIImage(named: "ico_delete"), forState: UIControlState.Normal)
    btnCall.addTarget(CommentViewController(), action: #selector(CommentViewController.confirmDelete), forControlEvents: UIControlEvents.TouchUpInside)

    viewCall.addSubview(btnCall)


    //Call the super addRightOptions to set the view that will display while swiping
    super.addRightOptionsView(viewCall)

}

func callButtonClicked(){
    //Reset the cell state and close the swipe action

    self.resetCellState()
}}

删除并添加新评论后,我总是调用重新加载表。

请指导我

2 个答案:

答案 0 :(得分:2)

您是否可以使用默认的iOS删除功能。但是不建议将图像添加到此默认删除按钮,只有文本可以更改。

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true // if deletable else false
}

func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
    return "Del"
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle = .Delete {
        // perform the delete and reload table
    }
}

答案 1 :(得分:0)

Try overriding cell's - (void)prepareForReuse method and remove this additional view there.

相关问题