my tableview is
struct country : Decodable {
let name : String
let capital : String
let region : String
}
class ViewController: UIViewController {
var countries = [country]()
let color = UIColor()
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
let jsonurl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonurl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.countries = try JSONDecoder().decode([country].self, from: data!)
} catch {
print("Error")
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}.resume()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func redButtonAction(_ sender: UIButton) {
let index = IndexPath.init(row: 0, section: 0)
let cell = tableview.cellForRow(at: index)
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return countries.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name.capitalized
let cellnumber = indexPath.row
return cell
}
现在我想在我的按钮操作中使用cellnumber或indexpath.row。我这样做了,但我无法获得 indexpath.row 当我
按下按钮时,表格视图的背景颜色变为红色,偶数单元格的背景颜色变为蓝色。但是问题是外侧表视图函数我只得到一个值而不是数组的整数。在上面的程序中,如果我打印单元格编号,我们会得到整数单元格。
表格视图单元格是可重用的,您只能获得可见单元格的 indexPath,因此您应该在单击按钮时重新加载表格视图,并且如果您想更改颜色或不在 cellForRowAt 中,则需要放置条件
单击按钮时更改行颜色的代码
struct country : Decodable {
let name : String
let capital : String
let region : String
}
class ViewController : UIViewController {
var countries = [country]()
let color = UIColor()
var needToChangeColor : Bool = false
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
let jsonurl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonurl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.countries = try JSONDecoder().decode([country].self, from: data!)
} catch {
print("Error")
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}.resume()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func redButtonAction(_ sender: UIButton) {
self.needToChangeColor = true
self.tableview.reloadData()
}
}
extension ViewController :UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return countries.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name.capitalized
if needToChangeColor == true {
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.blue
} else {
cell.backgroundColor = UIColor.red
}
} else {
cell.backgroundColor = UIColor.white
}
return cell
}
}
最基本的解决方案是在cellForRowAt
中为您的按钮添加一个tag
。如果您只有一个部分,它将起作用。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name.capitalized
cell.button.tag = indexPath.row
return cell
}
现在在您的方法中
@IBAction func redButtonAction(_ sender: UIButton) {
let index = IndexPath.init(row: sender.tag, section: 0)
let cell = tableview.cellForRow(at: index)
}
不幸的是,如果您向tableView
布局添加任何复杂性,这种方法将不会那么有效。