我的rootViewController包含一个带有自定义UICollectionViewCell的集合视图。我在单元格上有一个标签用作标题,它不应该改变,并且我有一个多行标签组成了其他单元格的正文。当我启动应用时,它会正确调用sizeToFit并使所有内容顶部对齐,因此它不是居中的。我点击其中一个单元格并进入下一个视图,然后如果我点击返回按钮回到rootViewController,则会运行viewWillAppear方法并重新加载数据,但sizeToFit不起作用,多行标签上的所有内容都变成居中对齐。它仍然是多行的,但如果只有几行,它会坐落在标签的中心位置,看起来不好。如何保持一致?我有一个左侧菜单,将重新加载rootViewController,这将再次正确定位标签,但是一旦我从secondViewController点击返回按钮后,它就不再对齐。是否有一种清除所有单元格并重新加载它们的方法?我已经尝试在viewWillAppear中使用[collectionView reloadData],但它不起作用,目前在connectionDidFinish的末尾调用它,网络连接是从viewWillAppear方法调用的。感谢您的协助。
以下是自定义UICollectionViewCell的代码。
以下是自定义UICollectionViewCell的代码。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
}
根视图控制器
-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"LocationListCell";
LocationList *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier
forIndexPath:indexPath];
[cell.titleBar setText:[value objectAtIndex:indexPath.row]];
NSArray *tempObject = [[NSArray alloc] initWithArray:[self getData:[key
objectAtIndex:indexPath.row]]];
NSMutableString *tempString = [[NSMutableString alloc] init];
for (int i = 0; i < [tempObject count]; i++)
{
[tempString appendString:[tempObject objectAtIndex:i]];
[tempString appendString:@"\r"];
}
//[cell.contentLabel sizeToFit];
//[cell.contentLabel setNumberOfLines:0];
[cell.contentLabel setText:tempString];
return cell;
}