作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在一个 View 中有一个表格布局作为自定义单元格,我面临的问题是里面的单元格有一个按钮我想在单击它时隐藏单元格中的按钮(只有被点击的那个应该被隐藏)我怎样才能以正确的方法做事?
滚动单元.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 项目我该怎么做?
最佳答案
有很多方法可以做到这一点。一种可能的解决方案是使用 block 。将此添加到 ScrollCell
var didLikedTapped: (() -> Void)?
并接收单元格中LikedButton的事件
@IBAction func LikeBtn(sender: AnyObject) {
didLikedTapped?()
}
然后在 viewController 的 cellForRowAtIndexPath 中添加这个
cell.didLikedTapped = {[weak self] in
print(IndexPath)
}
关于ios - 如何在 swift ios 中单击时更改单元格中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949587/
我是一名优秀的程序员,十分优秀!