所以这是一个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
}
答案 0 :(得分:1)
请使用以下UITableView
delegate
方法:
optional func tableView(_ tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int