Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIViewController内部带有按钮的表视图_Swift - Fatal编程技术网

Swift UIViewController内部带有按钮的表视图

Swift UIViewController内部带有按钮的表视图,swift,Swift,我试图使用UIViewController中包含单元格的表视图,我希望每一行中都有一个按钮 我之所以使用UIViewController而不是UITableView,是因为我希望在该视图中包含其他内容,而不是表视图所占据的整个屏幕。 我遇到的问题是,我在最后一个单元格中只看到一个按钮。我怎样才能解决这个问题,使每一行都有按钮? 我希望他们能用上这样的东西 import UIKit 类ViewController:UIViewController、UITableViewDataSource、UI

我试图使用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)
    }
}
}

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

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
}

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

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
}

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

    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)
    }

}

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

    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)
    }

}

您可以这样使用自定义单元格

使用子类
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
    }

}
现在,您可以在
cellforrowatinexpath
方法中使用自定义tableview单元格类创建自定义单元格:

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
}
您的结果将是:


查看示例了解更多信息。

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

使用子类
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
    }

}
现在,您可以在
cellforrowatinexpath
方法中使用自定义tableview单元格类创建自定义单元格:

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
}
您的结果将是:


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

事实上,这非常简单:您只需将UITableView按所需大小拖动到视图中即可。向其中添加一个原型单元,然后通过拖动标签等方式自定义该单元。创建一个新类,该类继承了前面介绍的UITableViewCell。您还可以将标签和按钮连接到类,如其他答案中所述。苹果有一个很好的解释,请转到他们解释如何定制单元格的部分。

事实上,这很简单:您只需将UITableView按所需大小拖动到视图中即可。向其中添加一个原型单元,然后通过拖动标签等方式自定义该单元。创建一个新类,该类继承了前面介绍的UITableViewCell。您还可以将标签和按钮连接到类,如其他答案中所述。苹果有一个很好的解释,请转到他们解释如何定制手机的部分。

谢谢大家提供的信息,也许我还不是100%确定。我不希望整个屏幕都是表格视图单元格。我希望我的表视图只占屏幕的1/2。我想在屏幕的顶部和底部有一些其他控件。这就是我最初使用UIViewControllerI的原因,我在我的示例中也这么做了谢谢大家提供的信息,也许我还不是100%确定。我不希望整个屏幕都是表格视图单元格。我希望我的表视图只占屏幕的1/2。我想在屏幕的顶部和底部有一些其他控件。这就是我最初使用UIViewControllerI的原因,在我的示例中也是如此