这就是我的手机的样子,目前:
它是UISplitVIEwController的一部分.这个问题是,之前由于某种原因,它会显示文本的整个长度,但它会到达单元格的末尾,而其余的字符串被切断(当我检查“autoLayout”时会发生这种情况).
这是我的代码目前的样子:
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static Nsstring *CellIDentifIEr = @"bcell"; BrackettableCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if(cell == nil) { cell = [[BrackettableCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:CellIDentifIEr]; [cell.description setlineBreakMode:NSlineBreakByWorDWrapPing]; cell.description.numberOflines = 0; cell.description.Font = [UIFont FontWithname:@"Helvetica" size:14.0]; } Bracket *bracket = [brackets objectAtIndex:indexPath.row]; [cell.description setText:bracket.name]; [cell.bracketID setText:[Nsstring stringWithFormat:@"%@",bracket.bracketID]]; return cell;}
我正在尝试身高,但这似乎并不重要,因为我可以将高度设置为任何值,但它仍然显示截断的文本.
谢谢!
解决方法 通常,我支持变量高度单元格的方法是定义一个类方法,该方法可以计算给定模型对象的大小:+ (CGfloat)heightForBracket:(Bracket*)bracket;
使它成为类方法的美妙之处在于,您可以与实际实现布局的代码共享常量(填充值,字体大小,缩进级别等),而不必将它们暴露给任何其他类.如果您希望将来更改这些常量,则只需在单元子类中的一个位置进行更改.子类实现的示例:
#define kpaddingHorizontal 10.0#define kpaddingVertical 10.0#define kFontSizename 17.0+ (CGfloat)heightForBracket:(Bracket*)bracket { // determine the dimensions of the name UIFont *nameFont = [UIFont systemFontOfSize:kFontSizename]; CGfloat nameSize = [bracket.name sizeWithFont:nameFont constrainedToSize:CGSizeMake(300,CGfloat_MAX) // 300 is the wIDth of your eventual label lineBreakMode:NSlineBreakByWorDWrapPing]; // Apple recommends all cells be at least 44px tall,so we enforce a minimum here return MAX(44,nameSize.height + 20 + kpaddingVertical*2); // 20 is space for the subTitle label}- (ID)initWithReuseIDentifIEr:(Nsstring *)reuseIDentifIEr { self = [super initWithStyle:UItableVIEwCellStyleSubTitle reuseIDentifIEr:reuseIDentifIEr]; if (self) { // bracket name self.textLabel.numberOflines = 0; // 0 makes this variable height self.textLabel.Font = [UIFont systemFontOfSize:kFontSizename]; self.textLabel.lineBreakMode = NSlineBreakByTruncatingTail; self.textLabel.backgroundcolor = [UIcolor clearcolor]; // if you wanted to hardcode a specific wIDth,to a subvIEw do it here as a constant and then share it with heightForBracket: // bracket number self.detailTextLabel.numberOflines = 1; self.detailTextLabel.Font = [UIFont systemFontOfSize:14.0]; self.detailTextLabel.lineBreakMode = NSlineBreakByTruncatingTail; self.detailTextLabel.backgroundcolor = [UIcolor clearcolor]; } return self;}- (voID)setBracket:(Bracket*)bracket { _bracket = bracket; self.textLabel.text = bracket.name; self.detailTextLabel.text = [Nsstring stringWithFormat:@"%@",bracket.bracketID];}
然后你可以在tableVIEw:heightForRowAtIndexPath ::中调用heightForBracket:
- (CGfloat)tableVIEw:(UItableVIEw *)tableVIEw heightForRowAtIndexPath:(NSIndexPath *)indexPath { Bracket *bracket = [brackets objectAtIndex:indexPath.row]; return [BrackettableCell heightForBracket:bracket];}
tableVIEw:cellForRowAtIndexPath:变得非常简单,只需在单元格上设置适当的括号:
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { static Nsstring *CellIDentifIEr = @"bcell"; BrackettableCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr]; if (cell == nil) { cell = [[BrackettableCell alloc] initWithReuseIDentifIEr:CellIDentifIEr]; } Bracket *bracket = [brackets objectAtIndex:indexPath.row]; cell.bracket = bracket; return cell;}
几点说明:
>这假设单元格未使用自动布局
>这明确地硬编码单元格/标签的宽度,这可能适合您的用例,也可能不适合您的用例
>你永远不应该命名属性描述,因为那是一个method that already exists on the NSObject
protocol>其他增强功能将缓存heightForBracket的结果:提高滚动性能,尤其是当您开始为大量子视图调整逻辑大小时
以上是内存溢出为你收集整理的ios – UITableViewCell中的文本换行不正确全部内容,希望文章能够帮你解决ios – UITableViewCell中的文本换行不正确所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)