我希望numberOfRowsInSection返回索引路径上的注释数量,并且只返回第一个单元格中的一个帖子,这将返回一个单元格用于发布,一个用于注释忽略var中的注释数量。 怎么了?
更新 这是一个详细信息视图控制器:当用户点击帖子查看评论时,它会显示第一个单元格中的帖子和评论。
我遇到问题第一个单元格没问题但是注释单元格只出现一个单元格,无论变量中有多少字符串出错了什么?
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
}
这是模拟器输出
答案 0 :(得分:2)
如果您希望您的帖子和评论是单个部分中的单元格,那么您的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
}
答案 1 :(得分:1)
根据您的上次更新判断,这是我认为您正在寻找的解决方案:
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 2 //One Section for the post and One Section for the comments?
}
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
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//post's section == 0
var cell: UITableViewCell!
if indexPath.section == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("postCID", forIndexPath: indexPath) as! postCell
cell.textLabel?.text = "fff"
}
else {
cell = tableView.dequeueReusableCellWithIdentifier("commentCID", forIndexPath: indexPath) as! commentCell
// Configure the cell...
cell.textLabel?.text = comments[indexPath.row]
}
return cell
}
答案 2 :(得分:1)
最后!
class CommentVC: UIViewController {
var comments = ["comments","comments","comments","comments","comments","comments"]
var posts = ["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.
//
// if (section == 0) {
// return posts.count
// }
// return comments.count
// }
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 2//One Section for the post and One Section for the comments?
}
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
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//post's section == 0
if indexPath.section == 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
}