如何获取尤里卡自定义单元格的 indexpath.row ,按钮标签
how to get eureka custom cell's indexpath.row , button tag
我制作了一个自定义单元格,在里面添加了一个按钮和标签。
删除单元格,点击按钮时,会pass array[Index]
,使用类似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(){
[=14=].value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)
[=14=].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(){
[=11=].value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)
[=11=].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
}
希望对您有所帮助,如果您对此有任何疑问,请告诉我,我会帮助您
我制作了一个自定义单元格,在里面添加了一个按钮和标签。
删除单元格,点击按钮时,会pass array[Index]
,使用类似button.tag = indexPath.row
,然后向服务器请求。
顺便说一句,这些功能在下面不起作用:
.onCellSelection{ cell, row in ...}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
also inside
.cellUpdate
,row.indexPath
? isnil
. 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(){
[=14=].value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)
[=14=].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(){
[=11=].value = Tmp_ReplyList(tripid: option.tripid, pinid: option.pinid, userid: option.userid, body: option.body, date: option.date, _id: option._id, userInfo: option.userInfo!)
[=11=].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
}
希望对您有所帮助,如果您对此有任何疑问,请告诉我,我会帮助您