简体   繁体   English

使用NSMutableArray数据填充UITableView自定义单元格-Swift

[英]Populate UITableView Custom Cell with NSMutableArray data - Swift

I am trying to populate a UITableView with some data that I have stored in an NSMutableArray (not sure if it should just be an Array?). 我试图用一些我存储在NSMutableArray中的数据填充UITableView(不确定是否应该只是一个Array?)。

The table consists of a custom cell which I have designed in the Storyboard which I have given the identifier: courseCell 该表包含一个我在情节提要中设计的自定义单元格,并为其指定了标识符:courseCell

To try and populate the UITableView with my data in my NSMutableArray, I was using this tutorial: http://www.codingexplorer.com/getting-started-uitableview-swift/ however it gets to a point where they are populating the cells using "cellForRowAtIndexPath", but they are just using the default cell style and so do the following: 为了尝试使用我的NSMutableArray中的数据填充UITableView,我使用了本教程: http : //www.codingexplorer.com/getting-started-uitableview-swift/,但是到了用它们填充单元格的地步“ cellForRowAtIndexPath”,但是它们只是使用默认的单元格样式,因此请执行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell

    let row = indexPath.row
    cell.textLabel?.text = swiftBlogs[row]

    return cell
}

But I am using a custom cell which has two labels. 但是我正在使用具有两个标签的自定义单元格。

I wasn't sure what to do, so I made an outlet for the labels and also the prototype cell: 我不确定该怎么做,所以我为标签和原型单元做了一个出口:

@IBOutlet var coursesTableView: UITableView!
@IBOutlet weak var customCell: UITableViewCell!
@IBOutlet weak var courseNameLabel: UILabel!
@IBOutlet weak var courseCodeLabel: UILabel!

I am a little lost and not sure how I can go through the cellForRowAtIndexPath function, loading in my NSMutableArray data into the two labels. 我有点迷茫,不确定如何通过cellForRowAtIndexPath函数将NSMutableArray数据加载到两个标签中。 Any help would be appreciated! 任何帮助,将不胜感激!

NOTE: I am only new to StackOverflow to please let me know if I have not been clear enough. 注意:我只是StackOverflow的新手,如果我不够清楚,请告诉我。

If you are using a custom UITableViewCell , then you need to create a class for that custom cell (and connect the cell with the class in storyboard). 如果使用自定义UITableViewCell ,则需要为该自定义单元格创建一个类(并将该单元格与情节提要中的类连接)。

This tutorial explains very well how custom cells work: LINK 本教程很好地说明了自定义单元格的工作方式: LINK

Note: Your problem is not with NSMutableArray , it's populating the custom cell. 注意:您的问题不是NSMutableArray ,而是填充自定义单元格。

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

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