我在一个视图中有一个表格布局,作为一个自定义单元格,我面临的问题是内部的单元格有一个按钮我想在单击它时隐藏单元格中的按钮(只有单击的按钮应该隐藏)我怎么能用正确的方法做事?
ScrollCell.swift
class ScrollCell: UITableViewCell {
@IBOutlet weak var ProfilePic: SpringImageView!
@IBOutlet weak var UserName: SpringButton!
@IBOutlet weak var Closet: UILabel!
@IBOutlet weak var Style: UILabel!
//------//
@IBOutlet weak var MianImg: UIImageView!
//-------//
@IBOutlet weak var ProductName: UILabel!
@IBOutlet weak var LoveCount: UIButton!
@IBOutlet weak var Discount: UILabel!
@IBOutlet weak var OrginalPrice: UILabel!
@IBOutlet weak var Unliked: UIButton!
@IBOutlet weak var Liked: UIButton!
@IBOutlet weak var Comment: UIButton!
@IBOutlet weak var Share: SpringButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override func layoutSubviews() {
ProfilePic.layer.cornerRadius = ProfilePic.bounds.height / 2
ProfilePic.clipsToBounds = true
}
}
ScrollController.swift
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1 // however many sections you need
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(try! Realm().objects(Feed))
var FeedModel = Feed()
let realm = try! Realm()
let tan = try! Realm().objects(Feed).sorted("ID", ascending: false)
return tan.count // however many rows you need
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// get an instance of your cell
cell = tableView.dequeueReusableCellWithIdentifier("ScrollCellDqueue", forIndexPath: indexPath) as! ScrollCell
IndexPath = indexPath.row
var FeedModel = Feed()
let realm = try! Realm()
let tan = try! Realm().objects(Feed).sorted("ID", ascending: false)
cell.ProfilePic.kf_setImageWithURL(NSURL(string:tan[indexPath.row].ProfilePic)!)
cell.UserName.setTitle(tan[indexPath.row].FullName, forState: .Normal)
cell.Style.text = tan[indexPath.row].StyleType
if tan[indexPath.row].UserType == "store_front"{
cell.Closet.text = "Store Front"
}else if tan[indexPath.row].UserType == "normal"{
cell.Closet.text = "Pri Loved"
}
//-----//
var SingleImage:String = ""
var ImageArray = tan[indexPath.row].ImageArraySet.componentsSeparatedByString(",")
SingleImage = ImageArray[0]
cell.MianImg.kf_setImageWithURL(NSURL(string:SingleImage)!)
//-----//
cell.ProductName.text = tan[indexPath.row].ItemName
cell.OrginalPrice?.text = "\(tan[indexPath.row].OrginalPrice)"
cell.LoveCount.setTitle("\(tan[indexPath.row].LikeCount)"+" Loves", forState: .Normal)
cell.Discount.text = "\(tan[indexPath.row].Discount)"+" % off"
if(tan[indexPath.row].LikeStatus){
cell.Unliked.hidden = true
cell.Liked.hidden = false
}
else if (!tan[indexPath.row].LikeStatus){
cell.Unliked.hidden = false
cell.Liked.hidden = true
}
cell.Unliked.tag = tan[indexPath.row].ID
cell.Liked.tag = tan[indexPath.row].ID
return cell
}
@IBAction func LikeBtn(sender: AnyObject) {
print(sender.tag)
print(IndexPath)
//here i want to know who i can hide the button i have clicked ?
}
在这里,我想访问单击按钮的单元格,并对该单元格内的UI项目进行更改,我该怎么办?
答案 0 :(得分:2)
有很多方法可以做到这一点。一种可能的解决方案是使用块。 将其添加到ScrollCell
var didLikedTapped: (() -> Void)?
并在单元格中接收LikedButton的事件
@IBAction func LikeBtn(sender: AnyObject) {
didLikedTapped?()
}
然后在viewController的cellForRowAtIndexPath中添加此
cell.didLikedTapped = {[weak self] in
print(IndexPath)
}
答案 1 :(得分:1)
检查以下代码是否有帮助
@IBAction func LikeBtn(sender: AnyObject) {
var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(position)
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath!)! as
UITableViewCell
print(indexPath?.row)
}
答案 2 :(得分:1)
给LikeBtn属性indexpath,在cellForRowAtIndexPath方法中,将indexPath传递给LikeBtn,然后你就会知道哪个单元格的LikeBtn被点击了。
class LikeBtn: UIButton {
var indexPath: NSIndexPath?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// here pass the indexpath to your button
cell.likeBtn.indexPath = indexPath
return cell
}
@IBAction func likeTap(sender: LikeBtn){
if let indexPath = sender.indexPath {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
//here you will know the exact cell, now you can hide or show your buttons
}
}
}
答案 3 :(得分:1)
喜欢ScrollCell中的uibutton,我不知道,为什么你可以在ScrollController中为它添加IBAction? 。您必须在ScrollCell和代码中实现它:
@IBAction func LikeBtn(sender: UIButton) {
print(sender.tag)
sender.hiden = true
}
我认为,如果你只有一个UI按钮,它会更好。在那里,喜欢和不同的是2状态的uibutton(seleted和none)。单击按钮时,更改其状态
更新:
class sampleCell: UITableViewCell{
@IBOutlet var btnLike : UIButton!
@IBOutlet var btnUnLike : UIButton! // frame of 2 button is equal
override func awakeFromNib() {
super.awakeFromNib()
self.btnUnLike.hidden = true
// ...
}
func updateData(data:AnyObject){ // data's type is Feed
// set data for cell
// i think you should implement in here. and in ScollController call : cell.updateData() , it's better
/* e.x
self.ProductName.text = tan[indexPath.row].ItemName
self.OrginalPrice?.text = "\(tan[indexPath.row].OrginalPrice)"
self.LoveCount.setTitle("\(tan[indexPath.row].LikeCount)"+" Loves", forState: .Normal)
self.Discount.text = "\(tan[indexPath.row].Discount)"+" % off"
*/
}
@IBAction func likeTap(sender:UIButton){ // rememeber set outlet event for btnLike and btnUnLike is this function
if sender == self.btnLike{
self.btnLike.hidden = true
self.btnUnLike.hidden = false
// do s.t
}else if sender == self.btnUnLike{
self.btnLike.hidden = false
self.btnUnLike.hidden = true
// do s.t
}
}
}