繁体   English   中英

用内容填充表格视图

[英]Populating Table View with content

我是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
}

好的,因此您需要了解的是上面显示的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是每个数组位置的完美模拟。 因此,数组和tableViews可以很好地结合在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM