我有一個現有的UITableView顯示數據,並正常工作。如何在Swift 3中將UIButton添加到我的UITableViewCell中?
但是我現在想要添加一個信息按鈕到這個UITableViewCell。
我將UIButton直接添加到故事板的TableViewCell中。然後我試圖聲明這個按鈕是一個插座,但我得到了錯誤
「插座不能連接到重複的內容。」
我讀圍繞主題,並決定建立一個名爲「新的子類
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
}
}
正如你可以看到我已經宣佈的UIButton personalStatsInfoButton
在這個子類。
隨着越來越多的閱讀圍繞主題我相信我需要添加如下內容:
personalStatsInfoButton.tag = indexPath.row
personalStatsInfoButton.addTarget(self, action: "infoButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
然後有一個函數:
function infoButtonClicked(sender:UIButton){
let infoCell = sender.tag
print (infoCell)
}
我的問題是我不知道我是否需要採取一切我現有的tableView代碼,並將其轉移到新的子類PersonalStatsTableViewCell
或只是與信息按鈕處理的部分。
下面是我現有的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()
}
}
正如Rajat所說,您可以在沒有自定義表格視圖單元類的情況下執行此操作。只需在cellForRowAt()方法中設置每個按鈕的目標/操作即可。然後,您可以使用該標記返回索引路徑,或者使用該按鈕的frame.origin來確定包含該按鈕的單元格的indexPath。 –