2017-04-05 49 views
1

我做了一個自定義單元格添加了一個按鈕,裏面的標籤。如何獲得尤里卡自定義單元格的indexpath.row,按鈕標籤

要刪除該單元格,點擊該按鈕時,將會出現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 

} 

刪除,點擊去按鈕時。

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 
} 

謝謝!

+3

如果可能的話,請讓你的問題更清楚一點,你的代碼。 – Sneha

+0

已添加!謝謝!! –

+0

@TahliaHyejin做了我的回答解決你的問題?,請讓我知道,最好的問候 –

回答

1

如果使用

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 

    } 

我希望這可以幫助你,如果你有任何問題與此,請讓我知道,我會幫你

+0

對不起,最近得到您的反饋。很好!!!非常感謝你!! –

相關問題