我有一个以编程方式创建的 UIPopoverController,它会弹出来自 .xib 的 UIViewController 的内容。UIViewController 包含一个(分组的) UITableView 和一个 UITableViewCell “原型”。当弹出框弹出时,将为所有需要的单元格复制单元格原型。单元格的数量超过了弹出框的大小,所以我可以滚动浏览所有单元格。
当弹出框弹出时,一切看起来都不错,单元格标签都左对齐。当我触摸 tableview 滚动时,所有单元格标签突然对齐到中心。当我将单元格向外滚动并向后滚动时,它们再次左对齐。在我将所有单元格向外滚动一次后,它们在余生中保持左对齐。我不知道是什么原因造成的……我的代码中没有任何内容可以改变单元格的对齐方式。这是一些代码:
UIViewController *editPopoverView = [[[ComponentViewEmptyEditPopover alloc] initWithNibName:@"ComponentViewEmptyEditPopover" bundle:nil] autorelease];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:editPopoverView];
aPopover.delegate = self;
[aPopover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
从弹出视图控制器
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *theCell = nil;
switch (indexPath.section)
{
case 0:
theCell = [tableView dequeueReusableCellWithIdentifier:@"typeCell"];
if (theCell == nil)
{
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self.typeCell];
theCell = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
switch (indexPath.row)
{
case 0:
[theCell.textLabel setText:@"Empty"];
break;
//
}
break;
}
return theCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1)
return 420;
return 44;
}
任何想法为什么会发生这种情况?我想这在某种程度上与细胞重用有关。
更新:就像我已经猜到的那样,它必须与重用有关。当我做
theCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"theCell"];
一切正常。但是,我希望能够通过界面生成器更改单元格的设计......
更新 2:如果我注释掉 dequeueReusableCellWithIdentifier 行,所有单元格在滚动后都会回到中心对齐。