在我的 swift
ios 应用程序中,我有一个 UITableView
- 在 Storyboard中,我添加了 3 个单元格。前两个是静态的,第三个是动态的,基本上前两个显示一些信息,第三个(以及基于第三个生成的其余部分)显示评论。我从我的网络服务器获取评论作为 json。
当 json 为空时,我看到单元格号。 1 和没有。 2 - 没关系。但是当 json 有一个值时,我只能看到单元格号。 1,我没有看到 2 或 3。当 json 有 2 条评论时 - 我看到两个静态单元格。 当 json 有 3 条评论时,我看到两个静态单元格 + 第 3 条评论。基本上我从来没有看到两条第一条评论。
问题可能出在这里 - 这就是我从网络服务获取评论的方式:
@IBOutlet weak var myTableView: UITableView!
var items = NSMutableArray()
....
Alamofire.request(.GET, "\(serverURL)/comments/\(case_id)/comments/")
.validate()
.responseJSON { response in
switch response.result {
case .Success:
self.items.removeAllObjects()
if let jsonData = response.result.value {
let data = JSON(jsonData)
if let responseDictionary = data.dictionary {
if let commentsArray = responseDictionary["comments"]?.array {
for commentObject in commentsArray {
if let singleComment = SingleComment.fromJSON(commentObject){
self.items.addObject(singleComment)
}
}
self.myTableView.reloadData()
}
}
}
self.myTableView.reloadData()
case .Failure(let error):
print("SWITCH ERROR comments")
print(error)
}
}
我的方法 numberOfRowsInSection
如下所示:
func tableView(tview: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.items.count == 0{
return 2
} else {
return self.items.count;
}
}
我认为解决方案可能是修改上面 else block 中的 return 语句,使其成为 return self.items.count+2
,但这会引发错误:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 0]'
遇到这种情况我该怎么办?
======编辑
我的 cellForRowAtIndexPath
是一个较长的方法,但我将其缩减以保留最重要的内容:
func tableView(myComments: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = myComments.dequeueReusableCellWithIdentifier("firstCell") as! FirstCell
...
return cell
}
else if indexPath.row == 1 {
let cell = myComments.dequeueReusableCellWithIdentifier("cellStatic") as! SecondCell
...
return cell
} else {
let cell = myComments.dequeueReusableCellWithIdentifier("cell") as! SingleCommentCell
let comment:SingleComment = self.items[indexPath.row] as! SingleComment
...
return cell
}
}
最佳答案
您需要在 numberOfRowsInSection
和 cellForRowAtIndexPath
函数中允许 2 个静态行。行数是 2 加上你的项目数
func tableView(tview: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count + 2;
}
当检索要显示的项目时,行值将比所需的 items
数组索引多两个,因此第 2 行将需要 item[0]
:
func tableView(myComments: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = myComments.dequeueReusableCellWithIdentifier("firstCell") as! FirstCell
...
return cell
}
else if indexPath.row == 1 {
let cell = myComments.dequeueReusableCellWithIdentifier("cellStatic") as! SecondCell
...
return cell
} else {
let cell = myComments.dequeueReusableCellWithIdentifier("cell") as! SingleCommentCell
let comment = self.items[indexPath.row - 2] as! SingleComment
...
return cell
}
}
此外,我建议您修改获取代码和数组定义,以便该项目是 var items = [SingleComment]()
而不是 NSMutableArray
。这将消除对从数组中检索到的对象进行向下转换的需要。
关于ios - 我在 UITableView 中混合了静态和动态 UITableViewCells,并在 numberOfRowsInSection 中得到了错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692436/