ios - 带有背景图像的 UItableviewcells 没有填满屏幕的整个宽度?

标签 ios objective-c uitableview

您好,我是 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

enter image description here

最佳答案

尝试将单元格的 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;

关于ios - 带有背景图像的 UItableviewcells 没有填满屏幕的整个宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35643312/

相关文章:

ios - 为什么水平 ScrollView 内容之间存在间隙?

iphone - 根据 UIWebView 是否正在加载,事件指示器不显示

ios - 交叉滚动 : tableview inside UIpageViewcontroller does not behave properly

ios - 发送到实例 Swift NSNotification 的无法识别的选择器

ios - 新的 iTunes Connect 无法在处理状态中选择我的应用构建

ios - 如何实现类似于 Hipmunk 的 iOS 日历选择器?

Objective-C 说明; -/+ 和 *var

ios - 在 Xcode 中调整图像大小?

ios - TableView didSelectRowAt IndexPath 没有被调用

ios - 自动布局,UiScrollView 内的 UITableView 不会增长