【问题标题】:table view cells with background image not filling the whole width of screen?带有背景图像的表格视图单元格没有填满屏幕的整个宽度?
【发布时间】:2016-02-25 13:55:51
【问题描述】:

嗨,我是 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

【问题讨论】:

  • 你打印了imgview框架和表格单元框架吗?
  • 你的类是否继承自UITableViewController

标签: ios objective-c uitableview


【解决方案1】:

对不起,我之前的错误帖子。您可能面临的问题可能是由于自动布局。检查图像视图是否分配了正确的自动布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2021-07-17
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多