[英]How do I add a UIButton into my UITableViewCell in Swift 3?
I have an existing UITableView that displays data and is working fine. 我有一个现有的UITableView,它可以显示数据并且工作正常。
However I now want to add an info button into this UITableViewCell. 但是,我现在想在此UITableViewCell中添加一个信息按钮。
I added the UIButton directly into the TableViewCell in storyboard. 我将UIButton直接添加到情节提要中的TableViewCell中。 I then tried to declare this button as an outlet but I got the error
然后,我尝试将此按钮声明为插座,但出现错误
"Outlets cannot be connected to repeating content."
“插座无法连接到重复内容。”
I read around the subject and decided to create a new subclass called " 我阅读了有关该主题的内容,并决定创建一个名为“
import UIKit
class PersonalStatsTableViewCell: UITableViewCell {
@IBOutlet weak var personalStatsInfoButton: UIButton!
var selectedCellTitle: String?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
As you can see I have declared the UIButton personalStatsInfoButton
in this sub-class. 如您所见,我已在此子类中声明了UIButton
personalStatsInfoButton
。
With more reading around the subject I believe I need to add something like: 通过阅读有关该主题的更多内容,我相信我需要添加以下内容:
personalStatsInfoButton.tag = indexPath.row
personalStatsInfoButton.addTarget(self, action: "infoButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
and then have a function: 然后有一个功能:
function infoButtonClicked(sender:UIButton){
let infoCell = sender.tag
print (infoCell)
}
My issue is I don't know whether I need to take all my existing tableView code and transfer it into the the new sub-class PersonalStatsTableViewCell
or just the parts that deal with the info button. 我的问题是我不知道我是否需要获取所有现有的tableView代码并将其转移到新的子类
PersonalStatsTableViewCell
或者仅将其转移到处理信息按钮的部分中。
Below is my existing VC code that initially deals with the TableView prior to adding in this new button. 以下是我现有的VC代码,在添加此新按钮之前,该代码最初处理TableView。
import UIKit
class ShowCommunityViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var membersTableView: UITableView!
@IBOutlet weak var communityName: UILabel!
var communityIsCalled: String?
var comIds = [String]()
var communityId: Int?
var selectedCellTitle: String?
var cellId: Int?
var communityPlayerIds = [String]()
var communityPlayers = [String?]()
override func viewDidLoad() {
super.viewDidLoad()
communityName.text = (communityIsCalled)
self.membersTableView.delegate = self
self.membersTableView.dataSource = self
membersTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.communityPlayers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalStatsTableViewCell", for: indexPath as IndexPath)
cell.textLabel?.text = self.communityPlayers[indexPath.row]
cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
cell.textLabel?.textColor = UIColor.white // set to any colour
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedCellTitle = self.communityPlayers[indexPath.row]
cellId = indexPath.row
}
override func viewDidAppear(_ animated: Bool) {
let myUrl = URL(string: "http://www.???.uk/???/specificCommunity.php?");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
let postString = "id=\(comIds[communityId!])";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
if error != nil {
print("error=\(error)")
return
}
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
if let arr = json?["players"] as? [[String:String]] {
self.communityPlayerIds = arr.flatMap { $0["id"]!}
self.communityPlayers = arr.flatMap { $0["user_name"]!}
self.membersTableView.reloadData()
print ("names: ",self.communityPlayers)
}
} catch{
print(error)
}
}
}
task.resume()
}
}
You don't need to put any code in your class PersonalStatsTableViewCell
you can manage all the things from ShowCommunityViewController
what you need to done is in your cellForRowAt
method add this 您不需要在类
PersonalStatsTableViewCell
放置任何代码,就可以从ShowCommunityViewController
管理所有事情, ShowCommunityViewController
您需要做的是在cellForRowAt
方法中添加此代码
cell.personalStatsInfoButton.tag = indexPath.row
cell.personalStatsInfoButton.addTarget(self, action: #selector(infoButtonClicked(sender:), forControlEvents: UIControlEvents.TouchUpInside)
and add this function 并添加此功能
function infoButtonClicked(sender:UIButton){
let infoCell = sender.tag
print (infoCell)
}
Your code and what you are thinking is correct, you just need to change the following line. 您的代码和您的想法是正确的,您只需要更改以下行。
Apart from what Arun B has said, you need to make sure xcode knows what kind of class cell
will belong to. 除了Arun B所说的以外,您还需要确保xcode知道属于哪种类型的类
cell
。
let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalStatsTableViewCell", for: indexPath as IndexPath)
should be 应该
let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalStatsTableViewCell", for: indexPath as IndexPath) as! PersonalStatsTableViewCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.