繁体   English   中英

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

[英]XCode7, Swift 2, Populating TableView with Parse data

我正在尝试为我的应用程序中的“公告”页面添加解析数据。 我已经设置了页面ViewController,添加了TableView,并按照步骤进行操作以使其准确打印正确的行数。 问题是文本本身。 我正在尝试将其连接到Header的UILabel,但无法正常工作。 任何帮助表示赞赏。

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


}

我看到你已经声明了一个变量

let reuseIdentifier = "ContentCell"

您使用的是正确的单元格吗?

如果您使用的是正确的单元格,并且已将HeaderText标签出口与AnnouncementCellTableViewCell正确连接(确保它与单元格连接而不是视图),并且您从Parse for Header获取值,则此方法应该起作用:

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
 }

希望这个帮助。

如果您使用故事板中tableView中制作的自定义单元格,我认为最简单的“ hoko up”标签的方法是在故事板的属性窗口中为其赋予tag号。

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

if let label = cell.viewWithTag(tag: tagNumber) as? UILabel {
    label.text = Header[indexPath.row]
}

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

// in or around viewDidLoad()
self.tableView.registerClass(AnnouncementCellTableViewCell.self, forCellReuseIdentifier: "AnnouncementBlock")

// in cellForRowAtIndexPath
let singleCell = tableView.dequeueReusableCellWithIdentifier("AnnouncementBlock", forIndexPath:indexPath) as AnnouncementCellTableViewCell

暂无
暂无

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

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