设备旋转后,如何让我的细胞重绘?
当我在iPhone上测试时,默认方向是纵向,所以我的动态单元格与设备纵向时需要的高度相同:这意味着当我旋转到横向时,会有很多空白区域.细胞.当我在iPad上测试时,情况正好相反.旋转到纵向时,我的部分文字会被切断,因为默认为横向.我为这两个设备分别设置了故事板,两个视图控制器都设置为"Orientation:Inferred"
(肖像)
(景观)
这就是iPhone上发生的事情 - 拉伸标签有深红色背景,因此您可以看到标签的实际尺寸.但是,单元格高度不会重新计算,并且由于底部标签被约束到单元格的底部,因此它们本身一直位于底部.如果我从拉伸标签(具有背景的标签)添加约束到单元格的底部,它所做的只是拉伸标签,如下所示:
(标签被拉伸)
这告诉我的是,现在我只是无法让单元格知道方向是什么,并且一旦它变得合适就重绘.这是我的heightForRowAtIndexPath代码计算单元格的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.opCell)
{
self.opCell = [self.container dequeueReusableCellWithIdentifier:@"OPCell"];
}
self.opCell.opName.text = [[self.data objectAtIndex:indexPath.row] valueForKey:@"name"];
[self.opCell layoutIfNeeded];
CGFloat height = [self.opCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height + 1;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,得到它的旋转我都试过[self.container reloadData],其中容器是我TableView与willRotateToInterfaceOrientation和didRotateToInterfaceOrientation无济于事.我也尝试将它放在viewDidLoad中,以便在景观模式下以正确的高度加载视图时检测并加载,但这也不起作用.对于iPhone,无论我做什么,它都会加载纵向视图的高度,并且不会在旋转时更新.
我发现了一些我认为可能会有帮助的问题,但我似乎无法让这个答案适合我,即使它看起来像我有同样的问题,而且这个问题似乎更多地关注单元格宽度而不是高度和我不确定它是否适用于同样的方式.
这是一个教程,当我在iPhone上运行它时,也没有在旋转时调整TableViewCells的大小.
本教程似乎对更改表高度有一些了解,因此我将begin/endUpdates代码添加到我的中didRotateToInterfaceOrientation,认为这可能会导致更改,但这不起作用.
所以,如果有人有任何建议或知道解决方案,我将非常感激,因为我正在用这个打砖墙!
objective-c tableviewcell ios heightforrowatindexpath autolayout
我的按钮看起来像横向的顶部图像:
截图http://i46.tinypic.com/33zb9dj.jpg
我希望它看起来像底部图像.
这是我的这些按钮的.xml代码.
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="1.0" >
<ImageButton
android:id="@+id/appHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="@null"
android:contentDescription="Home"
android:cropToPadding="true"
android:scaleType="fitEnd"
android:src="@drawable/home_bar" />
<ImageButton
android:id="@+id/menuHome"
android:layout_weight="0"
android:background="@null"
android:contentDescription="Menu"
android:cropToPadding="true"
android:scaleType="center"
android:src="@drawable/menu_bar" />
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="@null"
android:contentDescription="Back"
android:cropToPadding="true"
android:scaleType="fitStart"
android:src="@drawable/back_bar" />
</TableRow>
Run Code Online (Sandbox Code Playgroud) 我在这个问题中使用了Reuben Scratton的新答案代码.当我把它贴到我的代码,我得到红squigglies下 addOnGlobalLayoutListener(new OnGlobalLayoutListener(),onGlobalLayout()和activityRootView(heightDiff后).请帮我弄清楚出了什么问题.
谢谢这是我在.java页面上的代码
public class Details extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
}
final View activityRootView = findViewById(R.id.details);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
ImageButton backButton = (ImageButton)findViewById(R.id.back);
backButton.setImageResource(R.drawable.hide_keyboard);
}
}
});
Run Code Online (Sandbox Code Playgroud) 这里有一些工作代码,它在完成时为待办事项添加一个复选标记:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
} return cell;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是删除复选标记代码和类似的东西(它的基本逻辑):
if (toDoItem.completed) {
cellIdentifier.textLabel (NSAttributedString Strikethrough = YES)
} else {
cellIdentifier.textLabel (NSAttributedString Strikethrough = NO)
} return cell;
Run Code Online (Sandbox Code Playgroud)
我也试图改变NSString,以NSAttributedString和NSMutableAttributedString基于我已经看到了一些其他的问题和答案,如这一个是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary* …Run Code Online (Sandbox Code Playgroud) android ×2
ios ×2
objective-c ×2
autolayout ×1
button ×1
image ×1
layout ×1
listener ×1
nsstring ×1
padding ×1