为什么旋转到横向时此 UIImage 会从 UITableViewCell 中消失?
我正在尝试设置一个 UITableViewCell,它可以在右上角有一个图像。
我让它适用于纵向模式,但是当我旋转到横向模式时,图像消失了。
这是代码:
- (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] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *cornerImage = [UIImage imageNamed:@"star_corner.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - cornerImage.size.width,
0,
cornerImage.size.width,
cornerImage.size.height)];
imageView.tag = kCornerImageViewTag;
[cell.contentView addSubview:imageView];
[imageView release];
}
UIImageView *theView = (UIImageView *)[cell.contentView viewWithTag:kCornerImageViewTag];
[theView setImage: [UIImage imageNamed:@"star_corner.png"]];
cell.textLabel.text = @"the text label";
return cell;
}
有趣的是,如果我注释掉“cell.textLabel.text =”行,图像确实会以横向显示......尽管它不会移到最右侧。
如果我在这里做错了什么,请告诉我。
预先感谢您的任何帮助。
I'm trying to set up a UITableViewCell that can have an image in the upper right corner.
I have it working for portrait mode, but when I rotate to landscape, the image disappears.
Here's the code:
- (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] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIImage *cornerImage = [UIImage imageNamed:@"star_corner.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - cornerImage.size.width,
0,
cornerImage.size.width,
cornerImage.size.height)];
imageView.tag = kCornerImageViewTag;
[cell.contentView addSubview:imageView];
[imageView release];
}
UIImageView *theView = (UIImageView *)[cell.contentView viewWithTag:kCornerImageViewTag];
[theView setImage: [UIImage imageNamed:@"star_corner.png"]];
cell.textLabel.text = @"the text label";
return cell;
}
Interestingly, if I comment out the "cell.textLabel.text =" line, the image does show in landscape... although it doesn't shift over to the far right.
If there's anything else I'm doing wrong here, please let me know.
Thanks in advance for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我明白了这一点。解决方案是创建两个 UIImageView...一个用于星角图像,另一个用于单元格背景图像。
我将星角 UIImageView 作为子视图添加到背景图像 UIImageView,然后将其分配给 cell.backgroundView。
星形 UIImageView 通过以下方式正确定位自身:
由于整个内容都分配给 cell.backgroundView (而不是 cell.contentView),因此它不受单元格的删除按钮或重新排序控件的影响。
I figured this out. The solution was to create two UIImageView's... one for the star corner image, and one for the cell background image.
I added the star corner UIImageView as a subview to the background image UIImageView, then assigned that to cell.backgroundView.
The star UIImageView positions itself correctly by use of:
And since the whole thing is assigned to the cell.backgroundView (instead of cell.contentView), it isn't affected by the cell's delete button or reordering controls.
你的 UIViewController 应该响应 didRotateFromInterfaceOrientation。
在此方法中,您可以迭代单元格,使用 [cell viewWithTag] 获取 UIImageView 并根据新的单元格尺寸 (cell.contentView.bounds) 修改框架属性。
祝你好运!
Your UIViewController should respond to didRotateFromInterfaceOrientation.
In this method you can iterate through cells, get the UIImageView with [cell viewWithTag] and modify the frame property according to the new cell dimensions (cell.contentView.bounds).
Good luck!