Swift 在 UITableViewCell 中点击更改标签文本颜色
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
的颜色,我必须通过 let cell常量进入 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)
这会给你的细胞投射它作为你的细胞class
现在你可以用那个单元格中的标签做任何事情
cell.label.....
将标记添加到单元格 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
}
要先更改点击索引标签的颜色,您需要声明变量以识别点击位置
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
方法
自定义 Table 查看单元格:
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
}
对我来说,我希望标签的颜色在点击标签的容器单元格时发生变化。
您可以 select 为 Label 文本选择您想要的颜色,当被 selecting 轻触时,Label 会高亮显示(在属性检查器中)。从下拉菜单中,您可以 select 选择您希望在点击单元格时看到的颜色。
Attributes Inspector: Highlighted Property for label
我在 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
的颜色,我必须通过 let cell常量进入 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)
这会给你的细胞投射它作为你的细胞class 现在你可以用那个单元格中的标签做任何事情 cell.label.....
将标记添加到单元格 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
}
要先更改点击索引标签的颜色,您需要声明变量以识别点击位置
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
方法
自定义 Table 查看单元格:
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
}
对我来说,我希望标签的颜色在点击标签的容器单元格时发生变化。 您可以 select 为 Label 文本选择您想要的颜色,当被 selecting 轻触时,Label 会高亮显示(在属性检查器中)。从下拉菜单中,您可以 select 选择您希望在点击单元格时看到的颜色。
Attributes Inspector: Highlighted Property for label