Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 hereenter image description here

like image 670
Liam Weiland Avatar asked Apr 06 '14 06:04

Liam Weiland


2 Answers

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.

  • See also this Q&A.
like image 195
Suragch Avatar answered Sep 22 '22 06:09

Suragch


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

like image 24
CrimsonChris Avatar answered Sep 18 '22 06:09

CrimsonChris