[英]Swift Change label text color on tap from within TableViewCell
我有一個在TableView
的UILabel
,我想在用戶點擊時將UILabel的顏色更改為紅色。 我使用的是UITapGestureRecognizer
和敲擊UILabel
我能得到的內容UILabel
,但我不能得到實際UILabel
,因為據我所知,你不能有一個內部參數UIGesture
功能。
這是我的代碼,它將有助於清理
class HomeProfilePlacesCell: NSObject {
var Post = [String]()
@objc func PostTap(_ sender: UIGestureRecognizer) {
print(Post[(sender.view?.tag)!])
}
func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
tapGesture.delegate = self as? UIGestureRecognizerDelegate
cell.post.addGestureRecognizer(tapGesture)
cell.post.text = streamsModel.Posts[indexPath.row]
cell.post.tag = indexPath.row
Post = streamsModel.Posts
return cell
}
}
我的功能有PostTap每當用戶點擊UILabel
這是cell.post那么我可以讀取它里面PostTap但為了改變這種狀況的彩色內容UILabel
那么我將不得不在讓細胞不斷傳遞到PostTap功能。
無論如何我能做到這一點還是可以解決一下? 我是Swift的新手
使用TableView代理:[SWIFT 4.0]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath) as! <your Custom Cell>
cell.<your CustomCell label name>.textColor = UIColor.red
//OR
cell.<your Customcell label name>.backgroundColor = UIColor.green
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
func tableView(tableView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath) as! <your Custom Cell>
// change color back to whatever it was
cell.<your Customcell label name>.textColor = UIColor.black
//OR
cell.<your Customcell label name>.backgroundColor = UIColor.white
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
你可以通過使用它來實現
tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
當用戶點擊單元格時,此方法中調用的此方法執行此操作
tableView.cellForRow(at: indexPath)
這將給你細胞投射它作為你的細胞類,現在你可以用你的標簽在那個細胞細胞中做任何事情。標簽....
將標記作為indexPath.row添加到單元格
cell.tag = indexPath.row
然后
@objc func PostTap(_ sender: UIGestureRecognizer) {
let cell = self.tableVIew.cellForRow(at: sender.tag) as! HomeTVC
// Now you access your cell label here, and can do whatever you want
}
要首先更改單擊索引標簽的顏色,您需要在varible上聲明以標識單擊的位置
var selectedCellIndex = "" // initialize as empty string
你在cellForRowAt
func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
cell.post.text = streamsModel.Posts[indexPath.row]
cell.post.tag = indexPath.row
cell.post.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
tapGesture.delegate = self as? UIGestureRecognizerDelegate
cell.post.addGestureRecognizer(tapGesture)
Post = streamsModel.Posts
if self.selectedCellIndex == "\(indexPath.row)" {
cell.post.text = UIColor.red
} else {
cell.post.text = UIColor.blue
}
return cell
}
在您的Tap功能中
func PostTap(_ sender:UIGestureRecognizer){
let tapView = gesture.view!
let index = tapView.tag
self. selectedCellIndex = "\(index)"
self.YOUR_TABLE_NAME.reloadData()
}
希望對你有幫助
在Cell中嘗試Closure
方法 :
在自定義表視圖單元格中:
class HomeTVC: UITableViewCell {
@IBOutlet weak var labelPost: UILabel!
var callBackOnLabelTap: (()->())?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(postTap(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.delegate = self
self.labelPost.addGestureRecognizer(tapGesture)
}
@objc func postTap(_ sender: UIGestureRecognizer) {
self.callBackOnLabelTap?()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后在cellForRowAt indexPath
:
func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
cell.callBackOnLabelTap = {
cell.labelPost.backgroundColor = UIColor.black
}
return cell
}
對我來說,我想要在標簽的容器單元被輕敲時改變標簽的顏色。 通過選擇標記的突出顯示(在屬性檢查器中),可以選擇標簽文本所需的顏色。 從下拉列表中,您可以選擇在點擊單元格時要查看的顏色。
屬性檢查器:標簽的突出顯示屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.