I have custom cell of tableview which is used as header as well as normal cell.
In custom cell I have this properties
public var btnReplyTappedClousre:((CommentCell) -> (Void))?
public var btnBulbTappedClousre:((CommentCell) -> (Void))?
public var btnOtherReplyTapped:((CommentCell) -> (Void))?
And Call it with
@IBAction func btnReplyTapped(_ sender: Any) {
self.btnReplyTappedClousre?(self)
}
//--------------------------------------------------------------------------------
@IBAction func btnBulbTapped(_ sender: Any) {
self.btnBulbTappedClousre?(self)
}
//--------------------------------------------------------------------------------
@IBAction func btnViewPreviousReplyTapped(_ sender: Any) {
self.btnOtherReplyTapped?(self)
}
In view Controller i have implemented that closure
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: StoryBoard.Cells.CommentCell, for: indexPath) as! CommentCell
cell.isForHeaderCell = false
cell.btnReplyTappedClousre = {[weak self] (cell) in
self?.btnViewAllReplyTapped(cell)
}
cell.btnBulbTappedClousre = {[weak self] (cell) in
self?.btnBulbTapped(cell)
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: StoryBoard.Cells.CommentCell) as! CommentCell
cell.isForHeaderCell = true
cell.btnReplyTappedClousre = {[weak self] (cell) in
self?.btnViewAllReplyTapped(cell)
}
cell.btnBulbTappedClousre = {[weak self] (cell) in
self?.btnBulbTapped(cell)
}
cell.btnReplyTappedClousre = {[weak self] (cell) in
self?.btnReplyTapped(cell)
}
return cell
}
Is there any better way to do it ? Or can i improve it ?
isForHeaderCellbut in non header you forget to setcell.btnReplyTappedClousreto nil - because you can also get an reused cell from header \$\endgroup\$