Search code examples
swifteureka-forms

how to get eureka custom cell's indexpath.row , button tag


I made a custom cell added a button, labels inside.

to delete the cell, when clicking the button, it will pass array[Index], using like button.tag = indexPath.row, then request to the server.

by the way, those functions dont work below:

.onCellSelection{ cell, row in ...} 
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

also inside .cellUpdate, row.indexPath? is nil. hopefully could get the indexPath with the button.


custom cell

public class SnsReplyCell: Cell<Tmp_ReplyList>, CellType {

@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var replyUser: UILabel!
@IBOutlet weak var replyBody: UILabel!
@IBOutlet weak var delBtn: UIButton!

public override func setup() {
    height = { return 75 }
    row.title = nil
    row.value = nil
    super.setup()
    selectionStyle = .none

    profileImg.contentMode = .scaleAspectFill
    profileImg.clipsToBounds = true
    guard let tmp = row.value else { return } /
    replyUser.text = tmp.userid
    replyBody.text = tmp.body



}

public override func update() {
    super.update()
    guard let tmp = row.value else { return } 
    replyUser.text = tmp.userInfo.name
    replyBody.text = tmp.body
    profileImg.image = loadImageFromUrl(img_Url: tmp.userInfo.imageUrl).circle


}

}

public final class SnsReplyRow : Row<SnsReplyCell>, RowType {

required public init(tag: String?) {
    super.init(tag: tag)
    cellProvider = CellProvider<SnsReplyCell>(nibName: "SnsReplyCell")
}

}

config section,

var rows : [BaseRow] = []
var section1 = Section()


func replyFormConfig() -> Section{

    section1.header?.height = { 1 }
    self.tableView?.separatorStyle = .none

    for option in snsReplies {
        section1.append(SnsReplyRow(){

            $0.value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)


            $0.validationOptions = .validatesOnChange
            }.cellSetup({ (cell, row) in
                row.section?.form?.validate()

                cell.delBtn.addTarget(self, action: #selector(self.delAction(_:)), for: UIControlEvents.touchUpInside)

            }).cellUpdate { cell, row in


                cell.replyUser.text = option.userInfo?.name
                cell.replyBody.text = option.body
                cell.profileImg.image = loadImageFromUrl(img_Url: (option.userInfo?.imageUrl)!).circle

                // indexPath = nil below;
                cell.delBtn.tag = row.indexPath.row  

                cell.delBtn.addTarget(self, action: #selector(self.delAction(_:)), for: UIControlEvents.touchUpInside)

                if !row.isValid {
                    cell.replyUser.text = "is not valid"
                    cell.replyBody.text = option.body

                }
            }
            .onCellSelection { cell, row in
                row.section?.form?.validate()

                // indexPath = nil below too;
                cell.delBtn.tag = row.indexPath.row  
            }
        )//append


    }

    return section1

}

delete, when clicking the de button.

 func delAction(_ sender: UIButton){

   // sender.tag is cell.delBtn.tag below;
    let parameters = ["userid": snsReplies[sender.tag].userid, "replyid": snsReplies[sender.tag]._id]



    Alamofire.request(pom_url + "/sns/reply/delete", method: .post, parameters: parameters).responseJSON { (response) in
        print(response.result)

        switch response.result {
        case.success(let data):


            self.section1.remove(at: self.delBtnTag)

        case.failure:
            print("[sns/reply/delete] err")

            }
        }
    }




       func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.delBtn.tag = indexPath.row   //not Working
}

Thanks!


Solution

  • If you use

    for (index,option) in snsReplies.enumerated() {
     }
    

    you can use index as indexPath.row, and use it like here

     func replyFormConfig() -> Section{
    
            section1.header?.height = { 1 }
            self.tableView?.separatorStyle = .none
    
            for (index,option) in snsReplies.enumerated() {
                section1.append(SnsReplyRow(){
    
                    $0.value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)
    
    
                    $0.validationOptions = .validatesOnChange
                    }.cellSetup({ (cell, row) in
                        row.section?.form?.validate()
    
                        cell.delBtn.addTarget(self, action: #selector(self.delAction(_:)), for: UIControlEvents.touchUpInside)
    
                    }).cellUpdate { cell, row in
    
    
                        cell.replyUser.text = option.userInfo?.name
                        cell.replyBody.text = option.body
                        cell.profileImg.image = loadImageFromUrl(img_Url: (option.userInfo?.imageUrl)!).circle
    
                        // indexPath = nil below;
                        cell.delBtn.tag = index
    
                        cell.delBtn.addTarget(self, action: #selector(self.delAction(_:)), for: UIControlEvents.touchUpInside)
    
                        if !row.isValid {
                            cell.replyUser.text = "is not valid"
                            cell.replyBody.text = option.body
    
                        }
                    }
                    .onCellSelection { cell, row in
                        row.section?.form?.validate()
    
                        // indexPath = nil below too;
                        cell.delBtn.tag = index
                    }
                )//append
    
    
            }
    
            return section1
    
        }
    

    I hope this helps you, if you have any problem with this please let me know, I will help you