ios - 我要求的 Swift : Tableview numberOfRowsInSection didn't return the indexpath. 行

标签 ios swift uitableview

我希望 numberOfRowsInSection 返回索引路径处的评论数,并且仅在第一个单元格中返回一个帖子,这将返回一个用于帖子的单元格和一个用于评论的单元格,忽略变量中的评论数。 怎么了?

更新: 这是一个详细信息 View Controller :当用户点击帖子查看评论时,它会在第一个单元格中显示帖子,然后在下面显示评论。

我遇到一个问题,第一个单元格没问题,但注释单元格只显示一个单元格,无论变量中有多少个字符串出了什么问题?

  var comments = ["comments","comments","comments"]
        var posts = ["posts"]
        override func viewDidLoad() {
            super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    return 2
}


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    if indexPath.row == 0 {
         let cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
        cell.textLabel?.text = "fff"
    }


     let cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
    // Configure the cell...
    cell.textLabel?.text = comments[indexPath.row]

    return cell
}

我也尝试过这个 > 没用

       func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
       if (section == 0) {
         return 1
       }
        return comments.count
    }

这是模拟器输出

enter image description here

最佳答案

如果您希望帖子和评论成为单个部分中的单元格,则 numberOfRowsInSection 应为 posts.count + comments.count

如果您想要两个部分(一个用于帖子,一个用于评论),则需要从 numberOfSectionsInTableView: 返回 2 并使您的 tableView:numberOfRowsInSection: 看起来像您的编辑

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       if (section == 0) {
        return posts.count
       }
       return comments.count
 }

关于ios - 我要求的 Swift : Tableview numberOfRowsInSection didn't return the indexpath. 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775849/

相关文章:

ios - 返回 NULL 的 tableView 单元格

ios - ReactiveCocoa : sendNext to a subscriber in asynchronous programming

ios - 如何在键盘上添加完成按钮?

iphone - 从 iOS 上的 .net Web 服务获取数据

ios - 自定义 UITableViewCell 在 TableView 滚动时发生变化

ios - 在具有循环关系的 saveAllInBackground 期间解析 iOS SDK 死锁

ios - 如何从另一个controlview swift 3重新加载tableview?

ios - 为搜索栏委托(delegate)赋值 - Swift

ios - 使用 Material Library 中的 ErrorTextField 进行 Xcode UI 测试

ios - UITableViewController 更改一个可重用单元会影响其他单元