如何将UIImage集中在UITableView单元中?

时间:2014-11-20 22:40:02

标签: ios objective-c uitableview unrecognized-selector

我试图将所有UIImage图像集中在我的UITableView单元格中,但无法实现结果。

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

这是我的代码:

- (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次收到错误:

  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFConstantString   _isDecompressing]:无法识别的选择器发送到实例0x10036ada0'

我做错了什么?

3 个答案:

答案 0 :(得分:2)

您需要继承UITableViewCell并在layoutSubviews中重新定位内置imageView的框架,以实现您想要做的事情。你永远不应该在cellForRowAtIndexPath:中添加子视图到单元格,它会破坏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;
}

答案 1 :(得分:1)

你不能只调用cell.imageView.center吗?

NSString *image = [[NSBundle mainBundle]pathForResource:@"img" ofType:@".png"];
UIImage *img = [[UIImage alloc]initWithContentsOfFile:image];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake (cell.center.y, cell.center.x, cell.frame.size.width,     cell.frame.size.height);
cell.backgroundView = imgView;

答案 2 :(得分:0)

@pangu 如果你只是做了这样的事情来帮助拉伸图像呢?

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
}