ios - 用内容填充 TableView

标签 ios objective-c iphone swift tableview

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

相关文章:

iphone - 运输 iOS 蓝牙配件的成本数量级

iphone - 单击 UIToolBarButton 选中和取消选中 UITableViewCells

iphone - 在不同的 iOS SDK 下编译一个 iphone 应用程序

ios - 如何使用 iOS 10 UIViewPropertyAnimator 为圆形路径上的 View 设置动画

ios - 我正在使用 xcode 9。如何创建内部包含 xib 文件的框架?

ios - 发布版本的 dSYM 文件

ios - UIScrollView setContentSize 是如何工作的

ios - 使用方法调配立即更改所有 UIViewController 实例上 iOS13 上的 modalPresentationStyle

ios - Pod 版本控制深度

objective-c - 如何将字符串从 NSMutableArray 传递给 double