简体   繁体   English

如何在 Swift 3 中填充 tableView 自定义单元格?

[英]How to populate tableView custom cell in Swift 3?

I am loading news from the server in to the tableView.我正在从服务器加载新闻到 tableView。 Now I want to make a custom cell with headline, image and excerpt.现在我想制作一个带有标题、图像和摘录的自定义单元格。 I made a custom class for the cell and I gave the cell custom identifier.我为单元格创建了一个自定义类,并给出了单元格自定义标识符。 I put two labels (one for headline and one for excerpt).我放了两个标签(一个是标题,一个是摘录)。 Now I just want to display headline in the first label and excerpt in the second label.现在我只想在第一个标签中显示标题,在第二个标签中显示摘录。

       @IBOutlet weak var headline: UILabel!

       @IBOutlet weak var excerpt: UILabel!

//Custom struct for the data
struct News {
    let title : String
    let text : String
    let link : String
    let imgUrl : String

    init(dictionary: [String:String]) {
        self.title = dictionary["title"] ?? ""
        self.text = dictionary["text"] ?? ""
        self.link = dictionary["link"] ?? ""
        self.imgUrl = dictionary["imgUrl"] ?? ""
    }
}

//Array which holds the news
var newsData = [News]()

// Download the news
func downloadData() {
    Alamofire.request("https://api.sis.kemoke.net/news").responseJSON { response in
        print(response.request)  // original URL request
        print(response.response) // HTTP URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        //Optional binding to handle exceptions
        self.newsData.removeAll() // clean the data source array
        if let json = response.result.value as? [[String:String]] {
            for news in json {
                self.newsData.append(News(dictionary: news))
            }
            self.tableView.reloadData()
        }
    }
}

And in the following method I display the data in the cell在下面的方法中,我在单元格中显示数据

  override func tableView(_ tableView: UITableView, cellForRowAt     indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let news = newsData[indexPath.row]
    cell.textLabel?.text = news.title
    cell.detailTextLabel?.text = news.text
    return cell
 }

Create a subclass from UITableViewCell fe NewsCell .UITableViewCell fe NewsCell创建一个子类。

connect your outlets连接你的插座

@IBOutlet weak var headline: UILabel!
@IBOutlet weak var excerpt: UILabel!

to NewsCell .NewsCell In your override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {在你override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

function cast the cell like this:函数像这样投射单元格:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? NewsCell

and set your properties.并设置您的属性。

Make sure you set the class of the storyboard prototype cell as NewsCell.确保将故事板原型单元格的类设置为 NewsCell。

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

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