仅针对一个单元格进行缩进

时间:2015-04-19 10:26:32

标签: ios swift alamofire swifty-json

所以这是一个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
}

Projects screenshot Edited screenshot

1 个答案:

答案 0 :(得分:1)

请使用以下UITableView delegate方法:

optional func tableView(_ tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int