ios - 如何在 UITableView 单元格中居中 UIImage?

标签 ios objective-c uitableview unrecognized-selector

我试图在我的 UITableView 单元格中居中所有 UIImage 图像,但无法获得结果。

我尝试搜索建议/解决方案并找到了几个,但没有任何效果。

这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    cell.backgroundColor = [UIColor clearColor];

    cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];

    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath;
}

添加了以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:pic];
    pic.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

    ....
}

并且还尝试过:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [cell.contentView addSubview:pic];
    pic.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

    ....
}

并且:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    //cell.imageView.image = [UIImage imageNamed:[array objectAtIndex:indexPath.row]];
    UIImageView *pic = [ [UIImageView alloc] initWithImage:[array objectAtIndex:indexPath.row]];
    pic.center = CGPointMake(cell.contentView.bounds.size.width / 2 , 60);
    pic.contentMode = UIViewContentModeScaleAspectFit;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    [cell.contentView addSubview:pic];

    ....
}

所有 3 次都收到错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDecompressing]: unrecognized selector sent to instance 0x10036ada0'



我究竟做错了什么?

最佳答案

您需要继承 UITableViewCell 并在 layoutSubviews 中重新定位内置 imageView 的框架以实现您想要做的事情。您永远不应该将 cellForRowAtIndexPath: 中的 subview 添加到单元格,它会破坏 MVC。

static NSString * const CellIdentifier = @"CellIdentifier"

@implementation
....

// Add this in viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    ....

    [self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier];
}

试试这个:
// In a separate subclass of UITableViewCell

- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.center = CGPointMake(self.contentView.bounds.size.width/2,self.contentView.bounds.size.height/2);
}


// In your controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.backgroundColor = [UIColor clearColor];

    cell.imageView.image = [UIImage imageNamed:[sponsorsLogoArray objectAtIndex:indexPath.row]];

    return cell;
}

关于ios - 如何在 UITableView 单元格中居中 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050963/

相关文章:

iphone - 在 UITableView 中,取消屏幕外单元格的 GCD 操作的最佳方法是什么?

ios - 带阴影的 UITableView

ios - 修改 CellForRowAtIndexPath 中的一个单元格会更改多个单元格

jquery - 使用触发事件打开 iOS 原生 datePicker

ios - 无法从 iOS 应用程序使用 Spotify 登录

objective-c - 在运行时替换包中的图像

objective-c - 在资源中移动 .lproj 文件

ios - GKMatchmaker 自定义媒人 View Controller 已取消

javascript - 保留本地 UIWebView 值

ios - 默认实现中缺少 UITableViewCell detailTextLabel