Does the indentationLevel property seem to do nothing?

I have the number of rows that I insert into the table using -insertRowsAtIndexPaths: withRowAnimation, and I would like the rows to be indented on the left to distinguish them from the rest of the cells. The indentationLevel property for UITableViewCell is similar to what I need, but it seems to do nothing. Here is the code that I use to set the indentation level (the syntax of the point does not matter):

[cell setIndentationWidth:10]; [cell setIndentationLevel:1]; 

Is indentationLevel what I want, or do I need to look elsewhere?

+7
objective-c iphone
source share
6 answers

indentationLevel and indentationWidth affect the positioning of standard textLabel, detailTextLabel, and imageView elements. They do not affect the origin of the contentView frame.

If you are not using textLabel, etc. (possibly due to targeting on iPhone OS 2), you need to handle the indentation manually, as the previous answer shows.

+14
source share

This seems to be a bug in iOS 7 if you are not using custom cells.

Setting indentationLevel or indentationWidth inside tableView:cellForRowAtIndexPath: does not affect the layout of the cell when it is time to display it.

For example, consider the following code:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; BOOL isIndented = indexPath.row % 2; cell.textLabel.text = isIndented ? @"Indented" : @"Not indented"; cell.indentationLevel = isIndented; } 

I don't have enough reputation to post screenshots, but the result is different between iOS 6 and iOS 7. This is using Xcode 5 and iOS 7 SDK.

+3
source share

You can redirect your implementation to your cells. in

 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (cell == nil) { cell = (UITableViewCell*)[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellType01"] autorelease]; [cell setIndentationLevel:indexPath.row]; [cell setIndentationWidth:10]; cell.selectionStyle = UITableViewCellSelectionStyleNone; // Tag the view [[[cell subviews] objectAtIndex:0] setTag:111]; UILabel* labelView = [[UILabel alloc] initWithFrame: CGRectMake(cell.indentationLevel*cell.indentationWidth, 0, 300, 20)]; } } 
+1
source share

I had a similar problem with my application. I had a UITableView that did NOT use custom cells. I used a UITableViewCell with a basic style. In my delegate method - (UITableViewCell *)tableView: cellForRowAtIndexPath: I set the cell indentationWidth and indentationLevel . They did nothing. I even tried modifying the delegate method - (NSInteger)tableView: indentationLevelForRowAtIndexPath: Nothing.

Which ultimately worked in the storyboard and changed the attributes of the Table View itself. As soon as I set the indentation width there, I started getting the right indentation. The indentationLevel property has started to work. Even useful - (NSInteger)tableView: indentationLevelForRowAtIndexPath:

Basically, it seems that the indentationWidth property is ignored in favor of the Indentation Width in the storyboard.

This was with Xcode 5.0.2 targeting iOS 7.

+1
source share

Everything I read is talking about how you should do it. Make sure you do not create a new cell with the same pointer later in the code (after you set the indent, but before it is drawn). Have you tried to indent something more that would be more noticeable on the screen (say 100px instead of 10)?

0
source share

I know this question is 4 years old, but if you are already using a custom subclass of UITableViewCell and you are already adding your views as contentView make sure that you do not override layoutSubviews in your cell. If so, you need to manually edit the borders / center or contentView frame in layoutSubviews .

0
source share

All Articles