单元格上的图像不是一直向左 [英] Image on cells not all the way to the left

查看:22
本文介绍了单元格上的图像不是一直向左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 iOS 上的 UITableview 并学习在线课程.我得到的表格显示正常,但我的单元格上的图像并非一直向左(而教师的图像在).以下是相关单元格的屏幕截图:

我不想要那个间隙,我希望图像定位在单元格的开始处,一直到左边.我做了一些研究,似乎 Apple 已经更改了 ios6 和 ios7 之间单元格的默认外观,因此现在单元格中的图像在左侧显示出一点间隙.为了摆脱它,我尝试了 UIEdgeInsets:

[tableView setSeparatorInset:UIEdgeInsetsZero];

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

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

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

编辑-------------------------------------------------------------------------------------------------------------------------------

仍然没有找到答案.此处发布的解决方案不起作用.我找到了这段代码:

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

除了让我完全困惑之外(因为受影响的参数应该是 y?)我认为通过使单元格上的图像一直向左显示来解决问题,直到我意识到它只会将整个视图移动到左(正如我猜想的那样)在屏幕的另一侧留下相等的间隙.我想要的只是让我的单元格中的图像一直出现在单元格的左侧,就像以前的 ios 一样.谢谢

解决方案

通过创建子类 uitableviewcell(自定义类)还有另一种具体的方法来实现这一点.

要遵循的步骤

  • 创建一个 UITableViewCell 的子类.
  • 在 .h 文件中创建 UI 组件的属性和出口.
  • 转到故事板并在表格视图中添加表格视图单元格.
  • 现在添加 UI 组件,例如:图像视图或按钮等,并根据以下内容设置 x、y 值.
  • 使用身份检查器将自定义单元格类设为您的类名,请参见图片.
  • 连接 UI 组件的所有出口.

使用下面的代码uitableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{NSString *MyIdentifier = @"uniqueIdentifire";yourCustomClassForCell *cell = (yourCustomClassForCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];如果(单元格 == 零){cell = [[yourCustomClassForCell alloc] initWithStyle:UITableViewCellStyleDefault重用标识符:我的标识符];}cell.imageView.image = [imageAry objectAtIndex:indexPath.row];}

<块引用>

不要忘记通过使用故事板属性检查器 uniqueIdentifire 选择您的单元格来提供 identifireidentifire 属性查看图像.

您也可以通过在customeCellClass中添加以下代码(仅限方法)来在单元格之间留出一些垂直空间.

- (void)setFrame:(CGRect)frame {//在表格视图单元格之间插入间隙的方法frame.origin.y += 6;frame.size.height -= 2 * 6;[超级 setFrame:frame];}

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:

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

解决方案

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.
  • 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.

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];
}

这篇关于单元格上的图像不是一直向左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆