如何获取eureka自定义单元格的indexpath.row,按钮标记

时间:2017-04-05 10:35:24

标签: swift eureka-forms

我在自定义单元格中添加了一个按钮,里面有标签。

要删除单元格,单击按钮时,它将pass array[Index]使用button.tag = indexPath.row,然后请求服务器。

顺便说一下,这些功能在下面不起作用:

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

还在.cellUpdaterow.indexPath内?是nil。希望可以   使用按钮获取indexPath。

  

自定义单元格

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

}

  

配置部分,

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

}
  单击de按钮时

删除。

 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
}

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您使用

 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

    }

你可以使用index作为indexPath.row,并像这里一样使用它

<?php

return [
    'car' => [
          'BMW',
          'Audi',
          'Honda',
     ],
    'mob' => [
          'Apple',
          'Nokia',
          'Sony',
     ]
];

我希望这可以帮到你,如果你有任何问题请告诉我,我会帮助你