用内容填充表格视图 [英] Populating Table View with content

查看:57
本文介绍了用内容填充表格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone应用程序开发的新手.

I am new to iphone app development.

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

I currently have a table view but want to populate it with my own news stories any idea how I could go about this?

当前,我有以下内容:

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的单元格-就是所有这些.

OK so the thing you need to understand is the method cellForRowAtIndexPath you have shown above, is the method which is repeatedly called that populates the cells of a tableView - that's all there is to it.

因此,如果您具有以下实现方式:

Thus if you had the following implementation:

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".

Then every cell's textLabel would be populated with the word "hello".

在这一切上,您需要做的就是用要在tableView中显示的数据(即String名称,Date和其他一些数据的数组)填充一个数组(或其他对象),然后设置一个属性(代码标签或其他标签)具有 cellForRowAtIndexPath 中的数据的单元格.

Expanding on this all you need to do, is populate an array (or other object) with the data you want to display in the tableView (i.e. an array of String names, Dates, some other data) and then set a property (textLabel or other) of the cell with that data in cellForRowAtIndexPath.

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

The best bit is, each indexPath.row is a perfect analog for each array position. So arrays and tableViews go hand in hand nicely.

这篇关于用内容填充表格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆