Piraba
Piraba

Reputation: 7014

Swift Custom Swipe Table View Cell issue in dynamic

I have a list of comments. If it is my user own comment, when the user swipes to left to right , delete option will be enabled.After deleting the comments, it will reload the table. A user can add a new comment on the same page, that time also, it will reload the table.

I am using this repository and using this

Problem is that;

It is working but with few bugs.Let's explain the case. If it is not my comment also show the delete option when I swipe because I have added the new comment and delete some comments. in that case previous added swipe cell is not removed.I think that is the case. Could not realise it.

When I scroll the comment, it shows the delete option in the wrong manner. I have checked user id with the swipe. Please guide me. It was working in the normal way. Now current requirement was to show delete icon.

This is my code :

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

after deleting and adding the new comment, I always call the reload table.

Please guide me

Upvotes: 0

Views: 686

Answers (2)

Mr. Hedgehog
Mr. Hedgehog

Reputation: 986

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

Upvotes: 0

RJE
RJE

Reputation: 891

Can't you use default iOS delete for this. But it is not recommended to add images to this default delete button only text can change.

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

Upvotes: 2

Related Questions