简体   繁体   English

iOS表格视图

[英]iOS Table Views

Using just theTable Views, I added a label to the first cell, I tried to drag it to the view controller to ad code, but it didn't work. 我仅使用TableViews,在第一个单元格中添加了一个标签,然后尝试将其拖到视图控制器中以添加广告代码,但此方法不起作用。 (Still very new to programming) (对于编程还是很新的)

This is all that's in the view controller now. 这就是现在视图控制器中的全部内容。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell{
        return
    }
}

Also for this, I get "parameter requires an explicit type error 也是为此,我得到“参数需要显式类型错误

func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell {
    return
}

Provide a class to a view controller and you have to return a UITableViewCell. 向视图控制器提供一个类,您必须返回一个UITableViewCell。 once class is provided select lable and drag it to the file. 一旦提供了类,请选择标签并将其拖到文件中。

For the first part, be sure to assign the class of the view controller in the storyboards, otherwise, Xcode doesn't allow you to create @IBOutlets by dragging the label. 对于第一部分,请确保在情节提要中分配视图控制器的类,否则,Xcode不允许您通过拖动标签来创建@IBOutlets。

The second one, the function is expected to return a UITableViewCell , and you're not returning anything. 第二个,该函数应返回UITableViewCell ,并且您不返回任何内容。 Also, if you're using Swift 3, this is the new syntax. 另外,如果您使用的是Swift 3,则这是新语法。 A quick fix for you to check would be the following: 您可以通过以下快速解决方法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return UITableViewCell()
}

You have to create a class of type UITableViewCell 您必须创建一个UITableViewCell类型的类

  class sampleCell: UITableViewCell {
     @IBOutlet weak var _myLabel: UILabel! // you have to drag from  story board.
  }

//Storybord // Storybord

select the cell where you put the label. 选择放置标签的单元格。

click on the cell -> select identity inspecter -> custom class ->set the custom class name. 单击单元格->选择身份检查器->自定义类->设置自定义类名称。 // "sampleCell" //“ sampleCell”

select the next tab attribute inspector -> identifier set the name as "sampleCell" // you can put any name 选择下一个标签属性检查器->标识符将名称设置为“ sampleCell” //您可以输入任何名称

//ViewController // ViewController

set outlet tableview. 设置出口tableview。

     @IBOutlet weak var _tableView: UITableView! // drag from viewcontroller

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = _tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as! sampleCell
    cell._myLabel.text = "sample"
    return cell
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM