单元格上的图像并不全是左边的

我正在学习iOS上的UITableview,并在线学习课程。 我得到的表格显示得很好,但我的细胞上的图像不是所有的左边(而教师的)的方式。 这里是有问题的单元格的屏幕截图:

在这里输入图像说明

我不想要这样的差距,我想要把图像定位在单元格的开始,一直到左边。 我已经做了一些研究,似乎苹果已经改变了ios6和ios7之间的单元格的默认外观,所以现在单元格中的图像显示了左侧的一点空隙。 为了摆脱它,我尝试了UIEdgeInsets:

[tableView setSeparatorInset:UIEdgeInsetsZero]; 

这不起作用。 我也尝试过这种方法:

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

什么都没发生。 那我该怎么办呢? 谢谢

编辑 – – – – – – – – – – – – – – – – – – – – – – – – – ————————————————– —————————-

还没有find答案。 这里发布的解决scheme不起作用。 我发现这段代码:

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

除了完全迷惑我(因为参数受影响的应该是y),我想通过使单元格上的图像一直出现在左边来解决这个问题,直到我意识到它只是将整个视图向左移动(如我应该预计我猜)在屏幕的另一边留下一个相等的差距。 我所要的只是让单元格中的图像一直显示在单元格左侧,就像以前的ios一样。 谢谢

发生这种情况的原因是默认的表格内容偏移量是15,所以应该将其改为0.看到这一次,你有想法删除UITableView中的单元格之前的空白空间

如果您创build自定义单元格。 UITableViewCell拥有所有者imageView。 更改您的单元格中图像的标题。 如果您使用默认单元格,请使用带有约束的自定义单元格Leading space = 0。

最好不要使用单元格的默认imageView。 从目标库中拖放UIImageView,创build一个自定义的表格视图单元格(UITableViewCell的Child类),然后创build和拖动图像视图的出口。

UITableViewCell的间距是由于UITableViewCellshouldIndentWhileEditingRowAtIndexPath方法返回的默认TRUE

我能够重现你的问题 问题 由以下情况:

UITableView处于可编辑模式:

 self.tableView.editing = true 

你已经实现了:

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

要更正您的代码:如果您不想设置编辑样式,则可以通过closures编辑模式

 self.tableView.editing = false 

并删除editingStyleForRowAtIndexPath

否则,如果你需要编辑模式,然后设置适当的编辑风格( UITableViewCellEditingStyleDeleteUITableViewCellEditingStyleInsert )或简单地closures缩进。

 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { return FALSE; } 

您必须创build一个自定义的单元格,通过添加一个新的类作为UITableViewCell的子类。 那么你可以使用自动布局和约束条件来devise单元格来解决问题。

还有另外一个具体的方法来创build子类uitableviewcell(自定义类)。

步骤遵循

  • 创buildUITableViewCell的类子类。
  • 在.h文件中创buildUI组件的属性和出口。
  • 去故事板,并在tableview中添加表格视图单元格。
  • 现在添加UI组件,如:imageview或button等,并根据设置x,y值。
  • 使用身份检查器使自定义单元格的类成为你的类名。 在这里输入图像说明
  • 连接UI组件的所有sockets。

使用下面的代码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]; } 

不要忘记通过使用故事板select你的细胞给予识别属性检查器uniqueIdentifire识别属性见图像。

在这里输入图像说明

也可以通过在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]; } 

你不能真正改变uviewviewcell的内置子视图的框架,如imageview,accessoryview。 但是,如果你创build一个自定义的tableviewcell类(即使你不添加任何其他的子元素),你可以通过重写UITableViewCell中的layoutSubviews方法来改变内置的imageview框架。 我已经尝试过,它的工作原理。

 #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