如何设置Tableview的CustumCell高度编程?
我是iOS开发新手。 我做一个UITableview包含CustumCell.My CustumCell包含UITextView和一些其他控件但是我想设置我的单元格高度像300如果我的UITextViewtext包含一些文本,如果我的TextView文本是包含空文本意味着没有任何文本,那么我想我单元格高度170怎么可能。 我写了一个代码,但它不工作。
我的代码就像
NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"]; if([magazineDescription isKindOfClass:[NSString class]]) { cellOne.magazineDescriptionTextView.text = magazineDescription; cellOne.frame=CGRectMake(0, 0, 300, 300); } else { cellOne.magazineDescriptionTextView.text = @""; cellOne.magazineDescriptionTextView.hidden=TRUE; cellOne.frame=CGRectMake(0, 0, 300, 170); }
这里我self.firstArray包含JSON数据值和有时我的keyValue @“long_description”为空,所以我想隐藏TextView从单元格和设置单元格的高度从300到170和宽度都希望保持相同的两种情况下。
我知道像这样的方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
但是在这种方法中,如何设置此方法的高度,请给我解决scheme。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellText = @“Your text”; UIFont *cellFont = [UIFont systemFontOfSize:YourFontSize]; CGSize constraintSize = CGSizeMake(TextviewWidth, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping]; NSLog(@"labelSize : %f", labelSize.height); return labelSize.height; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Fetch your string from array as below or according to your code NSString *magazineDescription=[self.firstArray valueForKey:@"long_description"]; if(magazineDescription!=nil) { return 300.0f; } else { return 170.0f; } }
首先,向客户申报CustomerCell
@property (strong, nonatomic) CustomerCell *cell;
在tableViewDelegate中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([@"" isEqualToString:_cell.textView.text] || _cell.textView.text == nil) { return 170; } else { return 300; } }
希望它的帮助
- 对于viewForHeaderInSection中的自定义单元格,重新加载特定的viewForHeaderInSection IOS
- 如何调整UITableViewCell的大小以适应其内容?
- 如何保存uitableview中的复选标记,并在用户再次返回到视图时显示它们?
- UITableView设置隐藏的tableview行
- 如何实现在UITableView Cell中左右滑动,与iOS Mail类似
- Z UITableViewCell中图像和分隔符的索引
- 无法根据objective-c中标签的文本大小设置单元格大小
- Nib必须包含一个顶级对象,它必须是一个UITableViewCell实例
- 从UITableView删除最后一行 – 崩溃与NSInternalInconsistencyError