我希望 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
}
这是模拟器输出
最佳答案
如果您希望帖子和评论成为单个部分中的单元格,则 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/