返回带有标签的原型单元格
Returning a Prototype Cell with a Label
我正在尝试将标签连接到原型单元并且 return 它成功了。我这样做有些麻烦。我已连接我的 MyCustomTableViewCell。 xCode 检测到的应用程序中正式没有 "errors"。但是,当我 运行 它时,我只是返回一个空白的 table 视图。在代码的下方(倒数第二行),我放下 "print("Hi!")" 来检查代码是否正在处理,并且 "Hi!" 也没有出现在控制台中当 运行。这是我当前的代码:
import UIKit
class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var personText: UILabel!
}
class UserFeedTableViewController: UITableViewController, ComposeViewControllerDelegate {
private var posts: [PFObject]? {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Query for feeds
Downloader.sharedDownloader.queryForPosts()
// Add observers
NSNotificationCenter.defaultCenter().addObserver(self, selector: "queryFeeds:", name: queryNotification, object: nil)
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
// Notification SEL
func queryFeeds(notification: NSNotification) {
posts = notification.object as? [PFObject]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "postSegue" {
let nav = segue.destinationViewController as! UINavigationController
let composeVc = nav.topViewController as! ComposeViewController
composeVc.delegate = self
}
}
func dismissComposeViewController(viewController: ComposeViewController) {
dismissViewControllerAnimated(true, completion: nil)
}
func reloadTableViewAfterPosting() {
dismissViewControllerAnimated(true, completion: nil)
Downloader.sharedDownloader.queryForPosts()
}
}
// Mark Table View Data Source
extension UserFeedTableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as! MyCustomTableViewCell
// Configure the cell...
if let posts = posts {
let object = posts[indexPath.row]
cell.personText?.text = object["post"] as? String
cell.personText?.numberOfLines = 0
}
print("Hi!")
return cell
}
}
我怎样才能改变它,使其成为 return 单元格?
仔细检查 return posts?.count
。尝试 return posts?.count ?? 0
到 return 1
来测试
更新:我删除了我的错误。
希望这对您有所帮助。
我正在尝试将标签连接到原型单元并且 return 它成功了。我这样做有些麻烦。我已连接我的 MyCustomTableViewCell。 xCode 检测到的应用程序中正式没有 "errors"。但是,当我 运行 它时,我只是返回一个空白的 table 视图。在代码的下方(倒数第二行),我放下 "print("Hi!")" 来检查代码是否正在处理,并且 "Hi!" 也没有出现在控制台中当 运行。这是我当前的代码:
import UIKit
class MyCustomTableViewCell: UITableViewCell {
@IBOutlet weak var personText: UILabel!
}
class UserFeedTableViewController: UITableViewController, ComposeViewControllerDelegate {
private var posts: [PFObject]? {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Query for feeds
Downloader.sharedDownloader.queryForPosts()
// Add observers
NSNotificationCenter.defaultCenter().addObserver(self, selector: "queryFeeds:", name: queryNotification, object: nil)
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension
}
// Notification SEL
func queryFeeds(notification: NSNotification) {
posts = notification.object as? [PFObject]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "postSegue" {
let nav = segue.destinationViewController as! UINavigationController
let composeVc = nav.topViewController as! ComposeViewController
composeVc.delegate = self
}
}
func dismissComposeViewController(viewController: ComposeViewController) {
dismissViewControllerAnimated(true, completion: nil)
}
func reloadTableViewAfterPosting() {
dismissViewControllerAnimated(true, completion: nil)
Downloader.sharedDownloader.queryForPosts()
}
}
// Mark Table View Data Source
extension UserFeedTableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as! MyCustomTableViewCell
// Configure the cell...
if let posts = posts {
let object = posts[indexPath.row]
cell.personText?.text = object["post"] as? String
cell.personText?.numberOfLines = 0
}
print("Hi!")
return cell
}
}
我怎样才能改变它,使其成为 return 单元格?
仔细检查 return posts?.count
。尝试 return posts?.count ?? 0
到 return 1
来测试
更新:我删除了我的错误。
希望这对您有所帮助。