【问题标题】:Table View with buttons inside of UIViewControllerUIViewController 内带有按钮的表格视图
【发布时间】: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


    【解决方案1】:

    您可以通过这种方式使用自定义单元格。

    UITableViewCell的子类创建一个新的swift文件。

    通过选择您的单元格并转到Identity Inspector,将该类分配给您的单元格,它看起来像:

    并将元素添加到您需要的单元格中,例如,我根据您的需要在单元格中添加了两个标签和一个按钮,单元格将如下所示:

    在将该元素的连接出口连接到您的自定义调用中之后,您的自定义 tableview 单元格类将是:

    import UIKit
    
    class TableViewCell: UITableViewCell {
    
        @IBOutlet weak var titleLbl: UILabel!
        @IBOutlet weak var DetailLabel: UILabel!
        @IBOutlet weak var btn: UIButton!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
    }
    

    现在您可以在 cellForRowAtIndexPath 方法中以这种方式使用自定义表格视图单元格类创建自定义单元格:

    let myCell = mytableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    

    您可以通过这种方式为其赋值:

    myCell.titleLbl.text = carLocations[indexPath.row]
    myCell.DetailLabel.text = "Detailed Text"
    myCell.btn.tag = indexPath.row
    

    最后的代码是:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myCell = mytableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    
        myCell.titleLbl.text = carLocations[indexPath.row]
        myCell.DetailLabel.text = "Detailed Text"
        myCell.btn.tag = indexPath.row
    
        return myCell
    }
    

    你的结果将是:

    查看this 示例了解更多信息。

    【讨论】:

    • 感谢大家提供的信息,也许我仍然不能 100% 确定。我不希望整个屏幕成为表格视图单元格。我希望我的表格视图只占屏幕的 1/2。我想在屏幕的顶部和底部有一些其他的控制。这就是我最初使用 UIViewController 的原因
    • 我在我的例子中做了同样的事情
    【解决方案2】:

    在您的 tableview 上放置一个 UITableViewCell。这将为您提供自定义单元格外观的选项。创建一个继承自 UITableViewCell 的新类,并将其作为一个类添加到您的 tableview 单元格中。从单元格创建出口到这个新文件,然后使用 cellForRowAtIndexPath 设置单元格内控件的属性。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CBTableViewCell
        // add self as delegate for tablecell so delegate can call the function defined within
        cell.delegate = self
        cell.title.text = self.items[indexPath.row]
        return cell
    }
    

    【讨论】:

      【解决方案3】:

      我会使用自定义单元格来解决这个问题...

          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      
                  let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomCell
      
           //Do sth
      
      return cell 
      
      }
      

      你的手机:

      import UIKit
      
      class CustomCell: UITableViewCell {
      
          @IBOutlet var Button: UIButton!
      
          override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
              super.init(style: style, reuseIdentifier: reuseIdentifier)
          }
          required init?(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
          }
      
      
          override func awakeFromNib() {
              super.awakeFromNib()
          }
      
          override func setSelected(selected: Bool, animated: Bool) {
              super.setSelected(selected, animated: animated)
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        其实很简单:你只需将一个 UITableView 拖到你想要的大小的视图中。您向其中添加一个原型单元格,然后通过拖入标签等方式自定义该单元格。您创建一个新类,该类继承自 UITableViewCell,如前所述。您还可以将标签和按钮连接到类,如其他答案中所述。 Apple 有很好的解释here 转到他们解释如何自定义单元格的部分。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-22
          • 1970-01-01
          • 1970-01-01
          • 2012-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多