Liam Weiland
Liam Weiland

Reputation: 91

How can remove indentation in UITableView?

First of all, I am new at this and I am most likely forgetting something very simple.

Question:

I am making an application that displays random images from imgur.com in a tableView. For some reason all of the cells are indented a small amount as seen in the picture below. I have fiddled around with many settings in storyboard and have had no luck in fixing the issue.

notice indent on left side of images

Here is the tableView code...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return (_images.count * 2);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;

if (indexPath.row % 2 == 0) {
//content cell
    cell = [tableView dequeueReusableCellWithIdentifier:@"RandomImgurTableCell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:@"RandomImgurTableCell"];
    }

    long row = [indexPath row] / 2;

    SDImageCache* myCache = [SDImageCache sharedImageCache];
    cell.imageView.image = [myCache imageFromDiskCacheForKey:_images[row]];

}
else if (indexPath.row % 2 == 1) {
//separator cell
    cell = [tableView dequeueReusableCellWithIdentifier:@"SeparatorCell"];

    if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:@"SeparatorCell"];
    }
}
if (indexPath.row == (_images.count * 2) - 3) {
    [self checkToLoadMoreImages];
    NSLog(@"Hit bottom of table, getting more images");
}
return cell;
}

Here is a picture of my tableView and cell settings...

enter image description here enter image description here

Upvotes: 5

Views: 9692

Answers (4)

ttt
ttt

Reputation: 33

None of the above worked for me, I found this instead:

https://www.hackingwithswift.com/example-code/uikit/how-to-make-uitableviewcell-separators-go-edge-to-edge

You need to do two things. First, add these two lines of code to your table view controller's viewDidLoad() method:

tableView.layoutMargins = UIEdgeInsets.zero 
tableView.separatorInset = UIEdgeInsets.zero

Now look for your cellForRowAt method and add this:

cell.layoutMargins = UIEdgeInsets.zero

Upvotes: 1

Suragch
Suragch

Reputation: 511626

In order to get rid of the indent, you set the separator insets to zero. The result is shown in the following image.

enter image description here

In Code

myTableView.separatorInset = UIEdgeInsetsZero

In the Interface Builder

enter image description here

Notes

If the cells are causing the indent, then you can add the following lines to tableView:cellForRowAtIndexPath:

cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero

If you are still supporting pre iOS 8 then see this answer for more details.

Upvotes: 7

CrimsonChris
CrimsonChris

Reputation: 4641

Adjusting the separator inset should fix this. I believe that the default is 15px in from the left.

Upvotes: 5

Tom Susel
Tom Susel

Reputation: 3437

If you're referring to the horizontal indentation then you should try the following:

  1. Make sure that the content view frame x origin is at 0:

    yourTableViewCell.contentView.frame = CGRectMake(0.0f, 0.0f, SOME_WIDTH, SOME_HEIGHT);

  2. You should make sure that the image view you're adding to the UITableViewCell is aligned with it's parent left edge. If you're using autolayout than do it in the interface builder. Otherwise:

    yourImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;

  3. See that when the image is scaled, it remains true to the view frame:

    yourImageView.contentMode = UIViewContentModeScaleAspectFill;

  4. Make sure that the cell has 0 indentation:

    yourTableViewCell.indentationLevel = 0;

If none of this helps, set a breakpoint and examine the subviews of the cell using one of the following debugger command:

po [[UIWindow keyWindow] recursiveDescription]
po [yourTableViewCell recursiveDescription]

Upvotes: 1

Related Questions