gpt4 book ai didi

swift - 如何获取 Eureka 自定义单元格的 indexpath.row ,按钮标签

转载 作者:行者123 更新时间:2023-11-28 06:21:44 27 4
gpt4 key购买 nike

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

要删除单元格,点击按钮时,会传递array[Index],使用like button.tag = indexPath.row,然后向服务器请求.

顺便说一下,这些函数在下面不起作用:

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

谢谢!

最佳答案

如果你使用

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

您可以将索引用作indexPath.row,并像这里一样使用它

 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

}

希望对您有所帮助,如果您对此有任何疑问,请告诉我,我会帮助您

关于swift - 如何获取 Eureka 自定义单元格的 indexpath.row ,按钮标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com