大家好我只有13岁,住在德国,所以我提前为我糟糕的英语道歉。
我想通过单击集成在Cell中的UIButton来更改相应CustomCell的颜色
我已经尝试了所有我知道的东西,但它从未正常工作过。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var TableView: UITableView!
@IBOutlet weak var SearchBar: UISearchBar!
@IBOutlet weak var Banner: GADBannerView!
@IBOutlet weak var MarkButton: UIButton!
我将使用此按钮更改颜色" MarkButton"
var Array = ["1","2","3","4","5","6","7","8","9","10"]
var indexSearch = 0
var Filter = [String]()
var isSearching = false
override func viewDidLoad() {
super.viewDidLoad()
TableView.delegate = self
TableView.dataSource = self
SearchBar.delegate = self
SearchBar.returnKeyType = UIReturnKeyType.done
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return Filter.count
}else {
return Array.count
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = TableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
cell.CellLabel.text = Array[indexPath.row]
cell.selectionStyle = .none
if isSearching {
cell.CellLabel.text = Filter[indexPath.row]
}else {
cell.CellLabel.text = Array[indexPath.row]
}
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if SearchBar.text == nil{
isSearching = false
view.endEditing(true)
TableView.reloadData()
}else {
isSearching = true
searchBar.showsCancelButton = true
Filter = Array.filter({$0.contains(searchBar.text!)})
TableView.reloadData()
}
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isSearching = false
searchBar.text = nil
searchBar.showsCancelButton = false
searchBar.endEditing(true)
TableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if isSearching {
indexSearch = Array.index(of:Filter[indexPath.row])!
}else {
indexSearch = Array.index(of:Array[indexPath.row])!
}
switch indexSearch {
case 0:
print("0")
case 1:
print("1")
case 2:
print("2")
case 3:
print("3")
case 4:
print("4")
case 5:
print("5")
case 6:
print("6")
case 7:
print("7")
case 8:
print("8")
case 9:
print("9")
case 10:
print("10")
答案 0 :(得分:1)
广泛的步骤。 (应该有一个教程,你也应该使用它,因为这对于StackOverflow来说太宽泛了)
1)你的customCell类需要有一个你的按钮可以调用的IBAction
2)你需要将单元格的按钮连接到IBAction,这样你就可以获得它的点击
3)你告诉tableview的代表行动......例如通过自定义委托协议(-customCellWasClicked(self))
4)dataSource需要安全信息然后你调用reloadData(可以优化不重新加载)
5)在cellForRow方法中调整单元格的backgroundColor