簡體   English   中英

不執行靜態TableView Swift中cellForRowAtIndexPath中的if語句

[英]Does not Execute the if statement in cellForRowAtIndexPath in static TableView Swift

嗨,大家好,我一直在使用靜態tableView遇到問題。 我已經放置了一個動態的tableView來顯示評論。 但是問題是,如果我不將if語句放在cellForRowAtIndexPath中,則視圖不顯示任何內容..而當我使用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的新手

在此先感謝Aryan

設置視圖加載后的commentTableView的委托。

commentTableView.delegate = self

或將其連接到情節提要中(在視圖控制器中按住Ctrl並拖動到黃色圖標,選擇委托和數據源)

使用靜態TVC時,不會調用TVC的任何普通委托,其中包括numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPath 您應該從類中將其刪除,然后將需要執行的所有代碼遷移到其他函數,並且viewDidLoad是一個不錯的選擇,例如提到的@Shane。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM