我试图在我的 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/