0
サーバからのニュースをtableViewに読み込んでいます。今私は、見出し、画像、抜粋でカスタムセルを作りたいと思っています。 セルのカスタムクラスを作成し、セルのカスタム識別子を与えました。 私は2つのラベル(見出しのためのものと抜粋のためのもの)を入れました。 これで、最初のラベルに見出しを表示し、2番目のラベルで抜粋したいだけです。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
}
カスタムクラスの名前は何ですか?あなたはあなたのカスタムおもちゃとしてセルをキャストし、あなたが望むことをすることができるでしょう! –
例として、私はテーブルビューのセルカスタムクラスを呼び出すNewsCellを持っているので、私はそうします:cell = tableView.dequeueReusableCell(withIdentifier: "newsCell")? NewsCell –
詳細をお知らせください。これらのラベルをカスタムクラス "newsCellTableViewCell"に追加する必要がありますか? –