首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XCode7,Swift 2,使用解析数据填充TableView

XCode7,Swift 2,使用解析数据填充TableView
EN

Stack Overflow用户
提问于 2015-10-28 09:06:20
回答 3查看 222关注 0票数 0

我正在尝试为我的应用程序上的“公告”页面添加解析数据。我已经设置了页面ViewController,添加了TableView,并按照步骤进行操作,使其能够准确地打印正确的行数。问题在于文本本身。我试图将它连接到标题的UILabel,但它不工作。任何帮助都是非常感谢的。

代码语言:javascript
复制
import UIKit
import Parse
import Bolts

class ViewController: UIViewController, UITableViewDelegate {

@IBOutlet var tableView: UITableView!

var Header = [String]()




let reuseIdentifier = "ContentCell"
private let cellHeight: CGFloat = 210
private let cellSpacing: CGFloat = 20

@IBOutlet var barButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let navBar = self.navigationController!.navigationBar
    navBar.barTintColor = UIColor(red: 6.0 / 255.0, green: 100.0 / 255.0, blue: 255.0 / 255.0, alpha: 1)
    navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    var query = PFQuery(className: "Announcements")
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock {
        (posts:[PFObject]?, error: NSError?) -> Void in

        if error == nil {
            //success fetching announcements

            for alert in posts! {
                print(alert)
            self.Header.append(alert["Header"] as! String)
            }

            /***Reload The Table***/

            print(self.Header.count)

            self.tableView.reloadData()

        } else {
            print(error)

        }
}

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return Header.count
    //turns announcements into rows

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let singleCell: AnnouncementCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock") as! AnnouncementCellTableViewCell

    singleCell.HeaderText.text = Header[indexPath.row]

    return singleCell


}
EN

回答 3

Stack Overflow用户

发布于 2015-10-28 15:17:16

我看到你声明了一个变量

代码语言:javascript
复制
let reuseIdentifier = "ContentCell"

你是不是用对了手机?

如果您使用的是正确的单元格,并且使用AnnouncementCellTableViewCell正确连接了HeaderText标签出口(确保它与单元格连接,而不是视图),并且您正在从Parse for Header中获取值,那么这应该是可行的:

代码语言:javascript
复制
func tableView(tableView: UITableView, cellForRowAtIndexPath     indexPath: NSIndexPath) -> UITableViewCell{
      let cell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath: indexPath) as! AnnouncementCellTableViewCell
      cell.HeaderText.text = Header[indexPath.row]
      return cell
 }

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2015-10-28 09:54:42

如果你在故事板中使用在tableView中制作的自定义单元格,我认为最简单的方法是在故事板的属性窗口中给它一个tag编号。

然后在cellForRowAtIndexPath中获取带有标签的标签

代码语言:javascript
复制
if let label = cell.viewWithTag(tag: tagNumber) as? UILabel {
    label.text = Header[indexPath.row]
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-28 12:02:52

代码需要在您的自定义单元格类的tableView上调用registerClass:forCellReuseIdentifier:。要使单元出队,请使用dequeueReusableCellWithIdentifier:indexPath:

代码语言:javascript
复制
// in or around viewDidLoad()
self.tableView.registerClass(AnnouncementCellTableViewCell.self, forCellReuseIdentifier: "AnnouncementBlock")

// in cellForRowAtIndexPath
let singleCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath:indexPath) as AnnouncementCellTableViewCell
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33381219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档