【发布时间】:2015-04-19 10:26:31
【问题描述】:
所以这是一个 API。我有两个带有项目 ID([projects][id] - ids)和父母 ID([projects][parent][id] - projectsParentIDs)的字段。我在方法cellForRowAtIndexPath 中比较这两个字段,如下所示。到目前为止一切顺利(我认为)。如果索引为 1 的projectsParentID 与索引为 0 的id 相同,则缩进cell。但是这段代码正在缩进所有单元格。不止一个。我做错了什么?
for index in 0...self.projectsParentIDs.count-1 {
if(ids[index] == projectsParentIDs[index]+1){
cell.indentationLevel = 1
cell.indentationWidth = 30
}
}
"projects":[
{
"id":22,
"name":"MND",
"created_on":"2015-04-17T15:41:10+02:00",
"updated_on":"2015-04-17T15:41:10+02:00"
},
{
"id":23,
"name":"MND child",
"parent":{
"id":22,
"name":"MND"
}
],
"created_on":"2015-04-18T11:28:12+02:00",
"updated_on":"2015-04-18T11:28:12+02:00"
func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
var indent: Int = 1
if(indexPath.section == 1){
for index in 0...self.projectsParentIDs.count-1 {
if(ids[index] == projectsParentIDs[index]+1){
indent = 1
return indent
}else{
indent = 0
return indent
}
}
}
return indent
}
【问题讨论】:
-
这里的排序似乎很奇怪——我希望根项目排在第一位——有了这些数据,第一个项目将是一个孩子,因此会缩进,而第二个项目则不会。这是我对可能发生的事情的猜测。屏幕上显示的所有第一行都是子级。这些是唯一要分配的 tableview 单元格。当 tableview 来到第一个父级时,它会重用一个单元格-但如果项目是父级,您的代码不会将缩进重置为 0,因此它们都是缩进的。在 tv 子类上重置
prepareForReuse中的缩进或将if添加到cellForRow:
标签: ios swift alamofire swifty-json