【问题标题】:UItableviewcells with background image not filling the whole width of screen?带有背景图像的 UItableviewcells 没有填满屏幕的整个宽度?
【发布时间】:2016-02-26 04:10:37
【问题描述】:

嗨,我是 ios 新手,在我的应用程序中,我创建了一个 UITableView,并且我为 UITableViewcell 设置了背景图像,但图像没有填满屏幕的整个宽度,如下图所示。为什么会出现这个问题?

我的意思是UITableViewCell 左右两侧的间隙即将到来,图像没有填满整个单元格宽度。

请帮帮我

我的代码:-

#import "TableViewController.h"

@interface TableViewController ()
{
    UITableView * tableList;
    TableCell * Cell;
}

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    tableList = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, [[UIScreen mainScreen]bounds].size.height) style:UITableViewStylePlain];

    tableList.delegate = self;
    tableList.dataSource = self;
    tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableList];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"MyCell";
    Cell = (TableCell *)[tableList dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (Cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        Cell = [nib objectAtIndex:0];
    }

    //UIImageView *imageBackground = [[UIImageView alloc] init];

    if (indexPath.row == 0) {
        Cell.backGroundImage.image =  [UIImage imageNamed:@"cell_top.png"];
    } else if (indexPath.row == 9) {
       Cell.backGroundImage.image = [UIImage imageNamed:@"cell_bottom.png"];
    } else {
       Cell.backGroundImage.image = [UIImage imageNamed:@"cell_middle.png"];
    }

    //imageBackground.contentMode = UIViewContentModeScaleToFill;
    //Cell.backgroundView = imageBackground;

    return Cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44.0;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end

【问题讨论】:

  • 您有自定义 UITableViewCell 并从 loadNibNamed:@"TableCell" 加载它。从笔尖加载单元格时会出现此问题。为什么你覆盖 willDisplayCell 委托的乐趣?

标签: ios objective-c uitableview


【解决方案1】:

尝试将单元格的 layoutMargins 属性和UITableView 设置为UIEdgeInsetsZero

- (void) viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    tableList.layoutMargins = UIEdgeInsetsZero;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    [...]

    Cell.layoutMargins = UIEdgeInsetsZero;

    return Cell;
}

还要检查UIImageviewcontentMode

Cell.backGroundImage.contentMode = UIViewContentModeScaleAspectFill;

【讨论】:

    【解决方案2】:

    尝试在左侧设置 contentInset = 0

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

    【讨论】:

    • 不,不,它不起作用它如何成为我问题的答案?
    【解决方案3】:

    使用Debug View Hierarchy找出UITableView、UITableViewCell或UIImage没有填满整个屏幕宽度

    http://www.raywenderlich.com/98356/view-debugging-in-xcode-6

    【讨论】:

      【解决方案4】:

      请在情节提要中检查您的“TableCell”。您是否为自定义单元格选择了自定义插图选项?

      【讨论】:

        【解决方案5】:

        您不想使用代码设置表格视图,而是希望在情节提要中执行此操作。然后,您将需要使用自动布局将约束从表视图连接到视图控制器的视图。有很多教程可以教你如何做到这一点。从长远来看,学习这一点会让事情变得更容易。

        tableList 属性的名称更改为tableView。这对其他开发人员(包括将来的您自己)来说会更有意义,因为它就是这样(UITableView 实例)。

        您的单元格命名为Cell,大写字母C,但您不想使用大写字母命名属性。此外,它不需要像使用方式那样成为类属性。将其从 @interface 部分中删除。

        Coding Guidelines for Cocoa

        删除-numberOfSectionsInTableView: 方法。默认为 1,因此无需代码即可返回默认值。

        使用-dequeueReusableCellWithIdentifier:forIndexPath: 代替-dequeueReusableCellWithIdentifier:。然后,您无需通过测试来查看是否返回了单元格(它总是会返回)。您需要使用-registerNib:forCellReuseIdentifier: 注册您的笔尖。或者更好的是,只需在情节提要中进行设计。

        您的自定义表格视图单元格似乎有一个名为 backGroundImageUIImageView。这应该作为子视图添加到单元格的 backgroundView 属性(您需要创建它 - 视图,而不是属性,它已经是 UITableViewCell 的一部分)。设置图像视图的autoresizingMask,这样它就会使用backgroundView调整大小:

        - (void)awakeFromNib
        {
            [super awakeFromNib];
            self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
             self.backGroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
             self.backGroundImage.frame = self.backgroundView.bounds;
            [self.backgroundView addSubview:self.backGroundImage];
        }
        

        删除-tableView:heightForRowAtIndexPath: 方法。如果您返回不同的值,您只想使用它。默认行高为 44.0,因此您无需执行任何其他操作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-21
          • 1970-01-01
          • 2023-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多