我有一个代表提要的表格 View ,其中包含三个不同的自定义 UITableView 单元格。其中一个(顶部的)是实心的,应该始终存在,但其下方的单元格要么是产品,要么是事件单元格(从数据库加载)。问题是 Eventcell 有一个 TextView 和一个 ImageView ,它们的高度可能不同,因此为了正确查看这些 View ,我计算它们的正确高度,然后在 heightForRowAtIndexPath 中设置高度。我需要以某种方式更新单元格的新高度,因此我进行了表格 View 开始/结束更新。但是,当我每次将每个单元格加载到 View 中时都执行此操作时,当我滚动表格 View 时,所有单元格都会开始弹跳并更改内容。
这是我的 CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return [self loadJobInfoCell:indexPath];
} else if (indexPath.section == 1) {
if ([[jobDetailsArray objectAtIndex:indexPath.row] isKindOfClass:[JobProduct class]]) {
return [self loadProductCell:indexPath];
} else if ([[jobDetailsArray objectAtIndex:indexPath.row] isKindOfClass:[JobEvent class]]) {
static NSString *CellIdentifier = @"EventCell";
EventCell *cell = [tableViewRef dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[EventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
JobEvent *currentEvent = [jobDetailsArray objectAtIndex:indexPath.row];
// setting labels and stuff here
// Is there an image to this event?
if (![currentEvent.EventPicture isEqual:[NSNull null]]) {
[[cell largeImage] setImage:currentEvent.EventPicture];
[[cell largeImageHeightConstraint] setConstant:currentEvent.EventPicture.size.height];
NSNumber *height = [NSNumber numberWithFloat:currentEvent.EventPicture.size.height];
[largeImagesDictionary setObject:height forKey:[NSString stringWithFormat:@"%d", indexPath.row]];
} else {
[[cell largeImageHeightConstraint] setConstant:0.f];
}
// set correct height for the textview
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect paragraphRect = [cell.tvText.text boundingRectWithSize:CGSizeMake(204.f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil];
[[cell tvTextHeightConstraint] setConstant:paragraphRect.size.height+16.f];
NSNumber *height = [NSNumber numberWithFloat:[[cell tvTextHeightConstraint] constant]];
[eventTextHeightDictionary setObject:height forKey:[NSString stringWithFormat:@"%d", indexPath.row]];
[tableViewRef beginUpdates];
[tableViewRef endUpdates];
return cell;
}
}
return nil;
如果没有开始/结束更新,它可以正常工作,尽管单元格的高度不正确并且被剪切。我可以以某种方式更新高度而不重新加载表格,还是有更好的解决方案来解决整个情况?我尝试过跟踪哪些单元格已获得更新,但这不起作用,它仍然弄乱了顺序、高度和内容。我已经尝试了所有我能想到的解决方案组合,但作为一名 iOS 开发新手,我什至不确定是否采取了正确的方法来解决这个问题。
提前非常感谢。
编辑: 伙计,我很愚蠢..我已经坐下来计算了 cellforrowatindex 中的高度,然后在 heightforrowatindex 中计算了高度,并用 nsdictionaries 传递了数据。我通过自动布局并预先计算 heightforrowatindex 中数据的高度解决了这个问题。
最佳答案
我不太确定你的设置,但我这样做的方式是在 IB 中设置约束,以便 ImageView 和 TextView 将像单元格一样自动扩展。通过这种方式,我不必对代码中的 ImageView 或 TextView 进行任何大小更改,只需更改单元格大小即可。我的单元格设置如下所示:
ImageView 以 x 方向为中心,对单元格顶部有约束,对 TextView 有从下到上的约束。 TextView 对单元格的侧面和底部有约束。我在 ImageView 中放置了一个虚拟图像,并从编辑器菜单中选择了“大小以适合内容”——这会导致 ImageView 的高度和宽度约束被删除。
在代码中,我计算 ImageView 和 TextView 的大小,然后在 heightForRowAtIndexPath 中返回它们的高度总和(加上模糊因子)。以下是示例应用程序的代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = @[@{@"text":@"jkkjhkj kh k jk h hkj hjkhkjh hjkh jk hhkjhjkh jkh hkj hkjh hkjhjkhhkk jk jkh jkhkhkjhjhkjhkjhkkjhjjhk kjhkjh jkh hk h kj h jkh jkh kjh kh hjkhk jhjk", @"Pic":@"pic1.jpg"},@{@"text":@"fjhg lfkgh gjk gjk glkjfhgjkhgjkgh sjkghsjkgsjgjgk jgk hg hdgjlhjhjgjg fgjklfg fghjgk gjlkg hjgh jg jlkgljsdkggjlglgjdlkg hgjlgjfkghjg ljhfg jlskfdg hjgjlkgjlkdf gjfghjlkfgljkgjlkdgjdfghjdgjglhjkg hljkg ljkgljkfgljkgljksdgljkfgjlfg ljfglldkfjgh ljkgjlkf dgfghslfjdgklfjgljfdfgl", @"Pic":@"pic2.tiff"},@{@"text":@"jdkh lj flfh ljs fajlh ljds f", @"Pic":@"pic3.tiff"}];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textViewSize = [self.theData[indexPath.row][@"text"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(280.f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
UIImage *pic = [UIImage imageNamed:self.theData[indexPath.row][@"Pic"]];
CGSize imageViewSize = pic.size;
return textViewSize.height + imageViewSize.height + 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.tv.text = self.theData[indexPath.row][@"text"];
cell.iv.image = [UIImage imageNamed:self.theData[indexPath.row][@"Pic"]];
return cell;
}
关于ios - UITableViewCell : Begin/Endupdate causes cells to flip out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17631455/