Iphone 如何计算通过CellForRowatineXpath中的开关设置的单元格的HeightForRowatineXpath。
我的单元格设置如下: 因此,两个开关定义了单元格,因为数据不在对象列表中,而是一组信息,这些信息应该以不同的方式显示在tableView中Iphone 如何计算通过CellForRowatineXpath中的开关设置的单元格的HeightForRowatineXpath。,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我的单元格设置如下: 因此,两个开关定义了单元格,因为数据不在对象列表中,而是一组信息,这些信息应该以不同的方式显示在tableView中 -(UITableViewCell *)value1CellForTableView:(UITableView *)tableView { static NSString *CellIdentifierValue1 = @"Value1Cell"; UITableViewCell *cell = [tableView dequeueReusa
-(UITableViewCell *)value1CellForTableView:(UITableView *)tableView {
static NSString *CellIdentifierValue1 = @"Value1Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierValue1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierValue1];
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textAlignment = UITextAlignmentLeft;
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
switch (indexPath.section) {
case 0:
//Coupon
switch (indexPath.row) {
case 0:
//Couponcode
cell = [self value1CellForTableView:tableView];
cell.textLabel.text = @"Code";
cell.detailTextLabel.text = presentedCoupon.couponNr;
break;
case 1:
//Coupondescription
cell = [self value1CellForTableView:tableView];
cell.detailTextLabel.text = presentedCoupon.couponDescription;
cell.detailTextLabel.numberOfLines = 0;
cell.textLabel.text =@"Ihr Vorteil";
break;
}
break;
case 1:
//Productinfo
switch (indexPath.row) {
case 0:
cell = [self defaultCellForTableView:tableView];
cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: [presentedCoupon.refProdName stringByAppendingString:@".png"]]];
cell.textLabel.text = presentedCoupon.refProdName;
break;
}
break;
case 2:
//Shopinfo
switch (indexPath.row) {
case 0:
cell = [self defaultCellForTableView:tableView];
cell.textLabel.text = ((Shop*)presentedCoupon.refShop).name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
break;
}
if (cell == nil) {
cell = [self defaultCellForTableView:tableView];
cell.textLabel.text = @"Stanni";
}
[cell layoutIfNeeded];
return cell;
}
我这样计算高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"height");
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat height = 24 + [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize: CGSizeMake(cell.detailTextLabel.frame.size.width, 1000.0f) lineBreakMode:cell.detailTextLabel.lineBreakMode].height;
return MAX(height, 44.0f);
问题:
问题是,正如许多线程中提到的,在我的日志中也可以看到,在初始化表视图时会询问每个单元格的高度(是否可见)。因此,在更大的100+列表中,也会在启动时创建100+个单元格
当电池设置成这样时,是否还有其他可能获取此信息?是否真的有必要在heightForRowAtIndexPath中重新构建交换机机箱结构,以避免这些调用,并为每个单元获得正确的高度
保存一个包含每个单元格的单一信息的“数据源列表”会更好吗
但是如何处理不同的单元格样式、自定义单元格。方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
在方法之前为每个单元格调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
因此,在第一种方法中,单元格尚未创建,您不应该尝试访问它们
创建tableView时,首先要求数据源提供行数。然后,对于每一行,要求数据源提供行的高度,以便tableView知道其内容的总高度,最后,要求数据源显示单元格(实际视图)
在您的情况下,我会再次构建switch case结构。关于“数据源列表”,我以前从未这样做过,所以也许这是一个更好的解决方案。两种可能性:
NSArray
时,只需创建单元格即可然后,您可以从
表格视图:cellforrowatinexpath:
返回它们,或者测量它们的高度如我所见,您只有两种单元格类型。 因此,对于每种类型的单元格,您可能都有一个
@属性
(请查看下面的示例):
因此,单元格在开始时只会初始化一次。如果对象中有字符串数组,并且正在使用标准表格单元格,请尝试以下与iOS 7兼容的魔术:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* text = [self.objects objectAtIndex:indexPath.row];
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:text attributes:
@{ NSFontAttributeName: [UIFont systemFontOfSize:18]}];
//its not possible to get the cell label width since this method is called before cellForRow so best we can do
//is get the table width and subtract the default extra space on either side of the label.
CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 30, MAXFLOAT);
CGRect rect = [attributedString boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
//Add back in the extra padding above and below label on table cell.
rect.size.height = rect.size.height + 23;
//if height is smaller than a normal row set it to the normal cell height, otherwise return the bigger dynamic height.
return (rect.size.height < 44 ? 44 : rect.size.height);
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath{
NSString*text=[self.objects objectAtIndex:indexath.row];
NSAttributedString*attributedString=[[NSAttributedString alloc]initWithString:文本属性:
@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
//不可能获得单元格标签宽度,因为此方法在cellForRow之前调用,所以我们只能做到最好
//获取表格宽度并减去标签两侧的默认额外空间。
CGSize constraintSize=CGSizeMake(tableView.frame.size.width-30,MAXFLOAT);
CGRect rect=[attributedString boundingRectWithSize:constraintSize选项:(NSStringDrawingUserLineFragmentOrigin | NSStringDrawingUserFontLeading)上下文:nil];
//在表格单元格标签上方和下方添加额外的填充。
rect.size.height=rect.size.height+23;
//如果高度小于正常行,则将其设置为正常单元格高度,否则返回较大的动态高度。
返回值(rect.size.height<44?44:rect.size.height);
}
1。我的细胞很少。大概10点吧。所以你不会使用UITableView的重用功能,在viewDidLoad中设置单元格,并将它们保存在一个额外的数组中?但是,现在如何计算不同样式单元格的高度呢?2.这就像在一个额外的方法中再次构建开关盒,因为数据没有保存在列表或其他东西中。是否有比使用tableview更好的方法来表示数据?您的问题是,您正在从另一个委托方法调用一个委托方法。如果您事先创建了单元格,您可以在两种委托方法中使用它们,并且不会创建新对象。“或测量它们的高度”-要做到这一点,我首先必须强制单元格布局-此时它们甚至还没有添加到tableview中!我该怎么做?@de如果您知道单元格中应该显示哪些信息,请计算显示该信息所需的大小。您不需要依赖于布局。这就是为什么有所有的方法sizeWithFont:
和类似的方法。@Sulthan我理解这一点–但是,预先创建单元格有什么意义呢?我仍然需要手工计算高度,将tableViewStyle、单元格图像视图、单元格附件视图、细节标签等考虑在内。我希望通过你有前途的方法来解决这个问题。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* text = [self.objects objectAtIndex:indexPath.row];
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:text attributes:
@{ NSFontAttributeName: [UIFont systemFontOfSize:18]}];
//its not possible to get the cell label width since this method is called before cellForRow so best we can do
//is get the table width and subtract the default extra space on either side of the label.
CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 30, MAXFLOAT);
CGRect rect = [attributedString boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
//Add back in the extra padding above and below label on table cell.
rect.size.height = rect.size.height + 23;
//if height is smaller than a normal row set it to the normal cell height, otherwise return the bigger dynamic height.
return (rect.size.height < 44 ? 44 : rect.size.height);
}