6

I am learning about UITableview on iOS and following a course online. I get the table showing fine, but the images on my cells are not all the way to the left (whereas the instructor's ones are). Here is a screenshot of the cells in question:

enter image description here

I don't want that gap, I want the images to be positioned right at the beggining of the cell, all the way to the left. I have done some research and it seems Apple has changed the default look of the cells between ios6 and ios7 so that now the images in cells show a little gap at the left. To get rid of it, I have tried UIEdgeInsets:

[tableView setSeparatorInset:UIEdgeInsetsZero];

and that's not working. I also have tried this approach:

cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );

Nothing happens. So how would I go about it? Thanks

edit-------------------------------------------------------------------------------------------------------------------------------

Still not have found the answer to this. The solutions posted here don't work. I found this piece of code:

self.tableView.contentInset = UIEdgeInsetsMake(0, -50, 0, 0);

Which besides completely puzzling me (as the parameter affected should be the y?) I thought solved the issue by making the image on the cell appear all the way to the left, until I realised it only moved the whole view to the left (as I should have expected I guess) leaving an equal gap on the other side of the screen. All I want is for my images in the cells to appear all the way to the left of the cell as it used to be the case on previous ios. Thanks

Sunil Sharma
  • 2,653
  • 1
  • 25
  • 36
Paul
  • 1,277
  • 5
  • 28
  • 56

7 Answers7

0

It happens because default table content offset from left is 15, you should change it with 0. See this once, you get idea Remove empty space before cells in UITableView

Community
  • 1
  • 1
  • 2
    You should also put the code in your own answer, rather than only linking to someone else's. – kabiroberai Jul 23 '16 at 12:05
  • I have tried what the posts suggests, but self.automaticallyAdjustsScrollViewInsets = NO; doesn't do what I want. Matter of fact it makes things worse because now the table is hidden behind the top navigation bar. I don't want the whole table to behave differently, all I need is the images inside the cells to start at point 0. self.tableView.contentOffset = CGPointMake(0, 40); doesn't work either. I am surprised at how difficult this is, when it should be one line of code. – Paul Jul 23 '16 at 21:19
  • Try self.tableView.contentOffset = CGPointMake(0, 0); and in your cellForRowIndexPath method write these lines cell.separatorInset = UIEdgeInsetsZero; [cell setLayoutMargins:UIEdgeInsetsZero]; cell.preservesSuperviewLayoutMargins = NO; It should work as you you want. – i a m a g a m Jul 25 '16 at 18:06
0

If you create custom cells. UITableViewCell have owner imageView. Change title of image in your cell. If you use default cell, use custom cell with constraint Leading space = 0.

0

It is better not use default imageView of the cell. Drag and drop UIImageView from objective library, create a custom table view cell (Child class of UITableViewCell) then create and outlet of the image view just dragged.

Rokon
  • 355
  • 3
  • 11
0

The spacing in the UITableViewCell is because of the default TRUE returned by shouldIndentWhileEditingRowAtIndexPath method of UITableViewDelegate.

I was able to reproduce your problem problem by the below scenario:

UITableView is in editable mode:

self.tableView.editing = true

And you have implemented:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

To correct your code: If you do not want to set Editing Style then you can turn off the editing mode by

self.tableView.editing = false

and remove editingStyleForRowAtIndexPath.

Else if you need editing mode then set the appropiate Editing style(UITableViewCellEditingStyleDeleteor UITableViewCellEditingStyleInsert) or simply turn the indentation off.

- (BOOL)tableView:(UITableView *)tableView
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
 return FALSE;
}
SHN
  • 785
  • 7
  • 23
0

You must create a custom cell, by adding a new class as a subclass of UITableViewCell. then you can design cell with autolayout and constraints which will resolve the issue.

Surbhi Garg
  • 414
  • 5
  • 19
0

there is a another concrete way to achieve this by creating subclass uitableviewcell (custom class).

steps to follow

  • create a class subclass of UITableViewCell.
  • in .h file create properties and outlets of UI components.
  • go to storyboard and add table view cell inside the tableview.
  • now add UI components like: imageview or button etc and set the x, y values according to.
  • make class of custom cell your className using identity inspector see image. enter image description here
  • connect all outlets of UI components.

use below code uitableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *MyIdentifier = @"uniqueIdentifire";
    yourCustomClassForCell *cell = (yourCustomClassForCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil){
        cell = [[yourCustomClassForCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                 reuseIdentifier:MyIdentifier];

    }

    cell.imageView.image = [imageAry objectAtIndex:indexPath.row];
}

Dont forget to give identifire by selecting your cell using storyboard Attribute inspector uniqueIdentifire to identifire property see image.

enter image description here

Also you can give some vertical space between cells by just to add this below code (Method only) inside customeCellClass.

- (void)setFrame:(CGRect)frame {        // method to insert gap between table view cell
    frame.origin.y += 6;
    frame.size.height -= 2 * 6;
    [super setFrame:frame];
}
vaibhav
  • 4,038
  • 1
  • 21
  • 51
-1

You can not really change the frame of the inbuilt subviews of uitableviewcell like imageview, accessoryview. But if you create a custom tableviewcell class(even if you do not add any other subelement to it), you can change the frame of the inbuilt imageview by overriding the layoutSubviews method inside the UITableViewCell. I have tried it and it works.

#import "TableViewCell.h"

@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void) layoutSubviews{
    [super layoutSubviews];
    CGRect frame = self.imageView.frame;
    frame.origin.x = 0;
    self.imageView.frame = frame;
}

@end
Kunal
  • 227
  • 3
  • 9