可能有来自两个不同阵列的两个原型单元,没有部分?
Possible to have two prototype cells from two different arrays, without sections?
我正在努力实现这一目标,非常感谢您的帮助:
The first table is user posts, and when an item is selected, the 2nd tableview shows the selected user post ,在此下方,是对该 post 的任何评论,如果有的话(如 Facebook)。
我有两个没有部分的原型细胞。
- 我想从一个数组 (userPost) 输入到第一行
- 然后第二个原型单元格将包含来自单独注释数组的任何数据(如果有的话),这将是第一行之后的所有行。
- 目前两个原型电池都使用相同的自定义电池 class 和不同的插座,虽然我首先有两个自定义电池 classes,但它似乎不适用于代码示例我试过了。
我需要两个单独的数组,因为它们来自 cloudkit 中的两个不同的 table。而且我不想在提取 userPost 数据以显示在第一个 table 视图中的同时提取所有评论数据。
我在 SO 上找到了一些示例问题,但 none 对我的情况有用,因为它们略有不同。 none 也在 swift 中。我可以接受部分,但我无法在界面生成器中显示一个以上的部分,其中每个部分我可以有一个原型单元格。而且我真的不希望有部分,因为我不需要标题。
有人在 Swift 中对此有任何代码解决方案,特别是关于如何实现 "numberOfRowsInSection" 和 "cellForRowAtIndexPath"?
我已经为 "cellForRowAtIndexPath" 尝试了这两个示例,但没有成功:
// var cell: TableViewCell
if indexPath.row == 0 {
let cellPost = tableView.dequeueReusableCellWithIdentifier("CellPost", forIndexPath: indexPath) as TableViewCell
// custom cell class
let record = userPost[indexPath.row]
cellPost.postDescription.text = record.description
cellPost.userName.text = record.username
if record.postPic != nil {
cellPost.postPic.image = record.postPic!
}
cell = cellPost
} else {
let cellComment = tableView.dequeueReusableCellWithIdentifier("CellComment", forIndexPath: indexPath) as TableViewCell
let recordComment = commentResults[indexPath.row]
// could it be that its trying to subscript 1, but there's only data at 0?
cellComment.comment.text = recordComment.userComment
cellComment.commentUsername.text = recordComment.username
cell = cellComment
}
return cell
我试过了,没有成功:
var identifier: String!
if indexPath.row == 0 {
identifier == "CellPost"
} else {
identifier == "CellComment"
}
// then I configure the cell using the identifier as parameter.
在我试过的 numberofRowsinSection 中:
return feedResults.count + commentResults.count
//OR
return commentResults.count + 1
// OR
if commentResults.count > 0 && feedResults.count > 0 {
return feedResults.count + commentResults.count
} else {
return feedResults.count
}
我不断遇到的错误涉及第二个 commentResults 数组 - 要么说 - "cannot index empty buffer",要么说 "array index out of range"。我目前在数组中只有一个评论。不确定这是否导致了问题。
数据在那里,但不会拉入。非常感谢任何帮助!
当然,这是可能的。在 numberOfRowsInSection 中,return 评论的计数(我假设您在数组中有这些)加上一个(对于 post)。在 cellForRowAtIndexPath 中,使用 if-else 子句,如果 indexPath.row 等于零,则使用 return 一种单元格,如果不等于,则将另一种单元格出队。
我正在努力实现这一目标,非常感谢您的帮助:
The first table is user posts, and when an item is selected, the 2nd tableview shows the selected user post ,在此下方,是对该 post 的任何评论,如果有的话(如 Facebook)。
我有两个没有部分的原型细胞。
- 我想从一个数组 (userPost) 输入到第一行
- 然后第二个原型单元格将包含来自单独注释数组的任何数据(如果有的话),这将是第一行之后的所有行。
- 目前两个原型电池都使用相同的自定义电池 class 和不同的插座,虽然我首先有两个自定义电池 classes,但它似乎不适用于代码示例我试过了。
我需要两个单独的数组,因为它们来自 cloudkit 中的两个不同的 table。而且我不想在提取 userPost 数据以显示在第一个 table 视图中的同时提取所有评论数据。
我在 SO 上找到了一些示例问题,但 none 对我的情况有用,因为它们略有不同。 none 也在 swift 中。我可以接受部分,但我无法在界面生成器中显示一个以上的部分,其中每个部分我可以有一个原型单元格。而且我真的不希望有部分,因为我不需要标题。
有人在 Swift 中对此有任何代码解决方案,特别是关于如何实现 "numberOfRowsInSection" 和 "cellForRowAtIndexPath"?
我已经为 "cellForRowAtIndexPath" 尝试了这两个示例,但没有成功:
// var cell: TableViewCell
if indexPath.row == 0 {
let cellPost = tableView.dequeueReusableCellWithIdentifier("CellPost", forIndexPath: indexPath) as TableViewCell
// custom cell class
let record = userPost[indexPath.row]
cellPost.postDescription.text = record.description
cellPost.userName.text = record.username
if record.postPic != nil {
cellPost.postPic.image = record.postPic!
}
cell = cellPost
} else {
let cellComment = tableView.dequeueReusableCellWithIdentifier("CellComment", forIndexPath: indexPath) as TableViewCell
let recordComment = commentResults[indexPath.row]
// could it be that its trying to subscript 1, but there's only data at 0?
cellComment.comment.text = recordComment.userComment
cellComment.commentUsername.text = recordComment.username
cell = cellComment
}
return cell
我试过了,没有成功:
var identifier: String!
if indexPath.row == 0 {
identifier == "CellPost"
} else {
identifier == "CellComment"
}
// then I configure the cell using the identifier as parameter.
在我试过的 numberofRowsinSection 中:
return feedResults.count + commentResults.count
//OR
return commentResults.count + 1
// OR
if commentResults.count > 0 && feedResults.count > 0 {
return feedResults.count + commentResults.count
} else {
return feedResults.count
}
我不断遇到的错误涉及第二个 commentResults 数组 - 要么说 - "cannot index empty buffer",要么说 "array index out of range"。我目前在数组中只有一个评论。不确定这是否导致了问题。
数据在那里,但不会拉入。非常感谢任何帮助!
当然,这是可能的。在 numberOfRowsInSection 中,return 评论的计数(我假设您在数组中有这些)加上一个(对于 post)。在 cellForRowAtIndexPath 中,使用 if-else 子句,如果 indexPath.row 等于零,则使用 return 一种单元格,如果不等于,则将另一种单元格出队。