用内容填充表格视图

我是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中显示的数据(即string名称,date,一些其他数据),然后设置一个属性(textLabel或其他)与cellForRowAtIndexPath中的数据。

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