UIImage 未按预期显示
UIImage not shown as expected
- (void)showImageAtTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView indexPathsForSelectedRows].count) {
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) {
//cell is expanded
CGFloat frameHeight = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
CGFloat frameWidth = [[self.heightArray objectAtIndex:indexPath.row] floatValue];
//CGFloat yOrigin = [self configureHeightForRowAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(0,
12,
frameWidth,
frameHeight);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.image = [UIImage imageNamed:@"testImage.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//configue which cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:imageView];
}
}
}
上面的代码应该在点击时将 UIImageView 添加到现有的 UITableViewCell。这是调用该方法的行 (showImageAtTableView:forRowAtIndexPath) :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self updateTableView];
//get and show the image at selected cell
[self showImageAtTableView:tableView forRowAtIndexPath:indexPath];
}
但这是点击 tableviewcell 时的结果:
http://imgur.com/4NMJLf6,XweOgQu
我做错了什么?
您没有更改 UITableViewCell
的高度。
我认为更好的方法是将 UIImageView
包含在 xib
/ storyboard
设计中。假设您有某种 dto
对象存储单元格中显示的数据,您可以在它们上保留一个 boolean
指示是否应显示图像。
- 在您的
didSelectRowAtIndexPath
上,您将更新 boolean
,然后执行 [tableView reloadData]
。
- 在您的
cellForRowAtIndexPath
中,您将根据 boolean
. 隐藏/显示 UIImageView
- 在您的
heightForRowAtIndexPath
中,您将再次检查布尔值以相应地更新高度。
希望对您有所帮助。
- (void)showImageAtTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView indexPathsForSelectedRows].count) {
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) {
//cell is expanded
CGFloat frameHeight = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
CGFloat frameWidth = [[self.heightArray objectAtIndex:indexPath.row] floatValue];
//CGFloat yOrigin = [self configureHeightForRowAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(0,
12,
frameWidth,
frameHeight);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.image = [UIImage imageNamed:@"testImage.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//configue which cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:imageView];
}
}
}
上面的代码应该在点击时将 UIImageView 添加到现有的 UITableViewCell。这是调用该方法的行 (showImageAtTableView:forRowAtIndexPath) :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self updateTableView];
//get and show the image at selected cell
[self showImageAtTableView:tableView forRowAtIndexPath:indexPath];
}
但这是点击 tableviewcell 时的结果:
http://imgur.com/4NMJLf6,XweOgQu
我做错了什么?
您没有更改 UITableViewCell
的高度。
我认为更好的方法是将 UIImageView
包含在 xib
/ storyboard
设计中。假设您有某种 dto
对象存储单元格中显示的数据,您可以在它们上保留一个 boolean
指示是否应显示图像。
- 在您的
didSelectRowAtIndexPath
上,您将更新boolean
,然后执行[tableView reloadData]
。 - 在您的
cellForRowAtIndexPath
中,您将根据boolean
. 隐藏/显示 - 在您的
heightForRowAtIndexPath
中,您将再次检查布尔值以相应地更新高度。
UIImageView
希望对您有所帮助。