ios - 不执行静态 TableView Swift 中 cellForRowAtIndexPath 中的 if 语句

标签 ios swift uitableview

大家好,我一直遇到一个问题,我一直在使用静态 tableView。我已经放置了一个动态 tableView 来显示评论。但问题是,如果我不在 cellForRowAtIndexPath 中放置 if 语句, View 将不会显示任何内容。当我使用 if 语句时,代码不会通过 if block ,我也在其他项目中尝试过,并且使用 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 还很陌生

提前致谢 雅利安人

最佳答案

在 View 中设置 commentTableView 的委托(delegate)已加载。

commentTableView.delegate = self

或将其连接到 Storyboard 中(按住 Ctrl 键并拖动到 View Controller 中的黄色图标,选择委托(delegate)和数据源)

关于ios - 不执行静态 TableView Swift 中 cellForRowAtIndexPath 中的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832289/

相关文章:

ios - UITableView 单元格计数不足

iOS,绘制填充矩形的径向渐变

frameworks - 未加载 Swift 框架

Swift:不能使用存储的属性和 'required' 初始化程序 'init(coder:)' 必须由 'UIViewController' 的子类提供

ios - 输入邮政编码开关查看 Controller (如果在 Parse 中找到)(Swift 和 XCode)

ios - Swift 2 - 将数据从数组加载到 UITableView 中的单元格中

ios - 在 Swift 3 中选择 UITableView 单元格后将字符串设置为 var

ios - UITableViewCell 高度变化时闪烁

ios - 关系映射 Restkit

ios - 当设置为另一个 UITextField 的 inputAccessoryView 时,UITextField 高度不会改变