【问题标题】:Populating Table View with content使用内容填充表格视图
【发布时间】:2015-06-24 21:18:30
【问题描述】:

我是 iphone 应用程序开发的新手。

我目前有一个表格视图,但想用我自己的新闻故事填充它,知道我该怎么做吗?

目前我有以下:

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
}

【问题讨论】:

  • 对不起,我没有解释得很好。我想用来自 wordpress 的 rss 提要填充表格,以包含上述格式的图像。

标签: ios objective-c iphone swift tableview


【解决方案1】:

好的,所以您需要了解的是上面显示的方法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 中显示的数据(即字符串名称、日期、一些其他数据的数组)填充一个数组(或其他对象),然后设置一个属性(textLabel 或其他)在cellForRowAtIndexPath 中具有该数据的单元格。

最好的一点是,每个 indexPath.row 都是每个数组位置的完美模拟。所以数组和 tableView 可以很好地结合在一起。

【讨论】:

  • cellForRowAtIndexPath 不会被递归调用。也许您的意思是“反复调用”?
  • @ndmeiri 是的!对不起:)
  • 没问题!只是希望避免对碰巧仔细阅读您的完整答案的未来读者造成任何混淆:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多