0

So this is an API. I have two fields with ids of projects ([projects][id] - ids) and ids of parents ([projects][parent][id] - projectsParentIDs). I compare these two fields in the method cellForRowAtIndexPath like below. So far so good (I think). If projectsParentID with index 1 is the same as id with index 0, indent that cell. But this code is indenting all cells. Not only the one. What am I doing wrong?

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

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Kotora
  • 135
  • 1
  • 8
  • The ordering seems strange here- I'd expect the root projects to come first - with this data the first project would be a child and thus indented, and the second not. Here's my guess about what might be going on. All of the first rows to be shown on screen are children. These are the only tableview cells to be allocated. When the tableview comes to the first parent, it reuses a cell- but your code isn't resetting the indentation to 0 if the item is a parent, hence they are all indented. Reset the indentation in `prepareForReuse` on the tv subclass or add an `if` to `cellForRow:` – Rich Tolley Apr 19 '15 at 10:54

1 Answers1

1

Please use the following UITableView's delegate method:

optional func tableView(_ tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int
Azat
  • 6,745
  • 5
  • 31
  • 48
  • I know about this method, but how should I use it? Please see my edited question. It is still not working. It indent indexPath.section = 0 even if I have wrote indexPath.section = 1. And it is still indenting all cells. – Kotora Apr 19 '15 at 12:55
  • Do you use one of predefined `UITableViewCellStyle` or it is custom? If custom please check for example [that answer](http://stackoverflow.com/a/3878266/1565335) (you should handle indentation manually in that case) – Azat Apr 19 '15 at 13:07
  • Please look at my edited question. I have added screenshot of projects page. – Kotora Apr 19 '15 at 14:43
  • Maybe some mislogic in indent calculating code? Try to return something simple like `indexPath.section == 1 && indexPath.row == 0`. If that works as expected then you should debug your arrays more detailed – Azat Apr 19 '15 at 14:55
  • I see different indentation at the projects sections, looks like it works, isn't it? – Azat Apr 19 '15 at 16:42
  • Sadly no. Section 0 is indent but I don't want it because these are hard coded values (filtering my issues and close issues from API). Indented should be only projects MND child and MND child child (child of child). Project MND and MNDMND does not have a parent id and name. – Kotora Apr 19 '15 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75650/discussion-between-azat-and-kotora). – Azat Apr 19 '15 at 17:04