0
我從服務器中加載消息到tableView。現在我想用標題,圖片和摘錄製作一個自定義單元格。 我爲這個單元做了一個自定義的類,並給出了這個單元格的自定義標識符。 我放了兩個標籤(一個標題和一個摘錄)。 現在我只想在第一個標籤中顯示標題並在第二個標籤中顯示摘錄。如何在Swift 3中填充tableView自定義單元格?
@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()
}
}
}
而在下面的方法我顯示在細胞
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
}
什麼是您的自定義類的名稱?你應該將Cell作爲你的定製玩具,你將能夠做你想做的! –
例如我有一個表格視圖單元格自定義類叫做NewsCell所以我做:cell = tableView.dequeueReusableCell(withIdentifier:「newsCell」)as? NewsCell –
您能否提供更多信息?我應該在自定義類「newsCellTableViewCell」中添加這些標籤嗎? –