【发布时间】:2015-10-31 07:49:52
【问题描述】:
我正在尝试在 UIViewController 内使用带有单元格的表格视图,并且我希望每一行都有一个按钮。
我使用 UIViewController 而不是 UITableView 的原因是因为我想在该视图中包含其他内容,而不是表格视图占用的整个屏幕。
我遇到的问题是我在最后一个单元格中只看到一个按钮。我该如何解决这个问题,所以每一行都有按钮? 我希望可以使用这样的东西
import UIKit
类 ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var logButton: UIButton!
@IBOutlet weak var mytableView: UITableView!
let carLocations = ["Row One", "Row Two", "Row Three"]
override func viewDidLoad() {
super.viewDidLoad()
mytableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return carLocations.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell: UITableViewCell = mytableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
myCell.textLabel?.text = carLocations[indexPath.row]
myCell.detailTextLabel?.text = " Detailed text"
logButton.tag = indexPath.row
// I was hoping that I could use something like this
// myCell.logButton.tag = indexPath.row
return myCell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// handle delete (by removing the data from your array and updating the tableview)
}
}
【问题讨论】:
标签: swift