大家好,所以我一直遇到一个问题,我一直在使用静态tableView。我已经放了一个动态tableView来显示评论。但问题是,如果我没有在cellForRowAtIndexPath中放置一个if语句,那么视图就不会显示任何内容..当我使用if语句时,代码不会通过if块,我也在其他项目中尝试过并使用if语句唯一的方法..
所以ShotDetailTableViewController的代码就是这个
import UIKit
import FLAnimatedImage
class ShotDetailTableViewController: UITableViewController {
@IBOutlet weak var bgImageView: FLAnimatedImageView!
@IBOutlet weak var viewsCount: UILabel!
@IBOutlet weak var likesCount: UILabel!
@IBOutlet weak var commentsCount: UILabel!
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var reboundCount: UILabel!
@IBOutlet weak var attachmentCount: UILabel!
@IBOutlet weak var tagsCollectionView: UICollectionView!
@IBOutlet weak var reboundCollectionView: UICollectionView!
@IBOutlet weak var attachmentCollectionView: UICollectionView!
@IBOutlet weak var commentTableView: UITableView!
var shots : [Shot] = [Shot]()
var comments : [Comment] = [Comment]()
var shot : Shot!
var reboundPages = 1
var attachmentPages = 1
override func viewDidLoad() {
super.viewDidLoad()
title = shot.title
bgImageView.sd_setImageWithURL(NSURL(string: shot.imageUrl), placeholderImage: UIImage(named: "1"))
viewsCount.text = "\(shot.viewsCount)"
likesCount.text = "\(shot.likesCount)"
commentsCount.text = "\(shot.commentCount)"
avatarImageView.sd_setImageWithURL(NSURL(string: shot.user.avatarUrl), placeholderImage: UIImage(named: "2"))
usernameLabel.text = "\(shot.user.username)"
descriptionLabel.text = "\(shot.description)"
reboundCount.text = "\(shot.reboundCount)"
attachmentCount.text = "\(shot.attachmentsCount)"
let api = DribbleObjectHandler()
api.loadComments(shot.commentsUrl, completion:didLoadComments)
}
func didLoadComments(comments : [Comment]){
self.comments = comments
self.commentTableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
if tableView == commentTableView {
return 1
} else {
return 1
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if tableView == commentTableView {
return comments.count
} else {
return super.tableView(tableView, numberOfRowsInSection: section)
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == commentTableView {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CommentCell
// Configure the cell...
let comment = comments[indexPath.row]
cell.nameLabel.text = comment.user.name
cell.commentLabel.text = comment.body
cell.avatarImageView.sd_setImageWithURL(NSURL(string: comment.user.avatarUrl), placeholderImage: UIImage(named: "2"))
return cell
} else {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
请记住,我对Swift相当新鲜
先谢谢 雅利安
答案 0 :(得分:0)
在view do load中设置commentTableView的委托。
commentTableView.delegate = self
或在故事板中将其连线(在视图控制器中按住Ctrl键拖动到黄色图标,选择委托和数据源)
答案 1 :(得分:0)
当您使用静态TVC时,您的TVC的所有普通代表都不会被调用,其中包括numberOfSectionsInTableView
,numberOfRowsInSection
,cellForRowAtIndexPath
。你应该从你的类中删除它们并将需要执行的任何代码迁移到另一个函数,viewDidLoad
是一个好地方,就像@Shane提到的那样。