我正在尝试为我的应用程序上的“公告”页面添加解析数据。我已经设置了页面ViewController,添加了TableView,并按照步骤进行操作,使其能够准确地打印正确的行数。问题在于文本本身。我试图将它连接到标题的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
}发布于 2015-10-28 15:17:16
我看到你声明了一个变量
let reuseIdentifier = "ContentCell"你是不是用对了手机?
如果您使用的是正确的单元格,并且使用AnnouncementCellTableViewCell正确连接了HeaderText标签出口(确保它与单元格连接,而不是视图),并且您正在从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
}希望这能有所帮助。
发布于 2015-10-28 09:54:42
如果你在故事板中使用在tableView中制作的自定义单元格,我认为最简单的方法是在故事板的属性窗口中给它一个tag编号。
然后在cellForRowAtIndexPath中获取带有标签的标签
if let label = cell.viewWithTag(tag: tagNumber) as? UILabel {
label.text = Header[indexPath.row]
}发布于 2015-10-28 12:02:52
代码需要在您的自定义单元格类的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 AnnouncementCellTableViewCellhttps://stackoverflow.com/questions/33381219
复制相似问题