我是 iPhone 应用程序开发的新手。
我目前有一个表格 View ,但想用我自己的新闻故事填充它,知道我该怎么做吗?
目前我有以下内容:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsTableViewCell
// Configure the cell...
if indexPath.row == 0 {
cell.postImageView.image = UIImage(named: "premier-league")
cell.postTitleLabel.text = "Join our league"
cell.authorLabel.text = "Paul"
cell.authorImageView.image = UIImage(named: "author")
} else if indexPath.row == 1 {
cell.postImageView.image = UIImage(named: "example2")
cell.postTitleLabel.text = "Fixtures for the coming season"
cell.authorLabel.text = "Paul"
cell.authorImageView.image = UIImage(named: "author")
} else {
cell.postImageView.image = UIImage(named: "example3")
cell.postTitleLabel.text = "New App for the new season"
cell.authorLabel.text = "Paul"
cell.authorImageView.image = UIImage(named: "author")
}
return cell
}
最佳答案
好的,所以您需要了解的是上面显示的 cellForRowAtIndexPath
方法,该方法被重复调用以填充 tableView 的单元格 - 这就是它的全部内容。
因此,如果您有以下实现:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsTableViewCell
// Configure the cell...
cell.textLabel.text = "Hello"
return cell
}
然后每个单元格的 textLabel
将填充单词“hello”。
在此展开,您需要做的就是用要在 tableView 中显示的数据(即字符串名称、日期和其他一些数据的数组)填充一个数组(或其他对象),然后设置一个属性cellForRowAtIndexPath
中包含该数据的单元格(textLabel 或其他)。
最好的一点是,每个 indexPath.row 都是每个数组位置的完美模拟。所以数组和 tableView 可以很好地结合在一起。
关于ios - 用内容填充 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037237/