我有一个带有3个标签和一个按钮的自定义单元格。一切工作正常,直到我没有tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
为表格视图实现方法。(我需要它,因为我是根据标签的长度在此处设置单元格高度的。)问题是,当我点击时likeButton
,likeCountLabel.text
将在IB中显示IB的标签样本值一秒钟,然后再次显示前一个(正确的)字符串。每个单元,而不仅仅是在窃听中。当我删除heightForRowAtIndexPath:
它时,它又是完美的。正如我发现的那样,这是在重新加载表格视图后发生的,但是没有任何想法,因为当我不调整高度时,这些标签可以完美工作。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"cell";
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFQuery *query = [like query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects) {
NSString *likeNumber = [NSString stringWithFormat:@"%d", objects.count];
cell.likeCountLabel.text = likeNumber;
}
}];
cell.likeButton.tag = indexPath.row;
[cell.likeButton addTarget:self action:@selector(didTapLikeButton:) forControlEvents:UIControlEventTouchUpInside];
cell.usrName.text = object[@"usrName"];
cell.usrDescription.text = object[@"desc"];
cell.date.text = object[@"date"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
cell.usrName.text = object[@"usrName"];
cell.usrDescription.text = object[@"desc"];
cell.date.text = object[@"date"];
//get the height the cell
// [cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
//paddign of 1 Point
return height + 12;
}
- (void)didTapLikeButton:(id)sender {
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
PFObject *likeRow = [self.objects objectAtIndex:button.tag];
[SaveHelper likeObject:likeRow withUploader:[PFUser currentUser]];
[self.tableView reloadData];
}
您不应该在其中出队或实例化单元格heightForRowAtIndexPath:
-这会破坏模式(即:tableView
要求一定的单元格高度,indexPath
另一方面,您要求它为相同的实例化单元格,indexPath
以便可以回答所需的高度。 ..)
在cellForRowAtIndexPath:
一个单元中,根据数据模型生成(或重用)并填充数据。如果像元高度取决于数据-该高度也应从数据模型计算得出。
为了避免实例化单元格,heightForRowAtIndexPath:
您可以编写类似的类方法,+ (CGFloat) heightOfCellWithContent...
然后使用此方法计算内某种类型的单元格所需的高度heightForRowAtIndexPath:
。
换句话说:您需要NewsTableViewCell
类以能够告诉您具有特定内容的实例(该类型的单元格)所需的高度...
编辑:
你显然有一个UITableViewCell
子类,命名NewsTableViewCell
和这个类的实例希望用数据来填充像usrName
,usrDescription
...
将类方法添加到NewsTableViewCell
:
+ (CGFloat)cellHeightNeededForUsrName:(NSString *)usrName
usrDescription:(NSString *)usrDescription {
// calculate the height here
}
甚至更好:
+ (CGFloat)cellHeightNeededForData:(PFObject *)data {
// calculate the height here
}
然后- (CGFloat)tableView:heightForRowAtIndexPath:
以这种方式修改您的内容:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
PFObject *object = [self.objects objectAtIndex:indexPath.row];
return [NewsTableViewCell cellHeightNeededForData:object];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句