[英]IOS:Creating a Self-Sizing UITextView Within a UITableViewCell?
I am using stroyboards for my project. 我正在为项目使用滑板。 My project is based on parsing json datas and displaying it in JSON.
我的项目基于解析json数据并将其显示为JSON。 While parsing JSON I will receive two different datas.
解析JSON时,我将收到两个不同的数据。
Title and Description
i have to parse this and have to fix in UItextview inside a uitableviewcell
i have structured a uitableviewcell like this Title and Description
我必须解析它,并且必须在UItextview inside a uitableviewcell
修复,我已经像这样构造了uitableviewcell
TEXTVIEW,TEXTVIEW,LABEL,LABEL,LABEL,LABEL,LABEL; TEXTVIEW,TEXTVIEW,LABEL,LABEL,LABEL,LABEL,LABEL; Now my problem is my text view has to resize according to data, and based on that tableviewcell also has to resize I tried the above code but the view gets collapsing.
现在我的问题是我的文本视图必须根据数据调整大小,并且基于该tableviewcell也必须调整大小,我尝试了上面的代码,但视图崩溃了。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Need_Request";
NeedReqListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
type = (NSString *)[array4 objectAtIndex:indexPath.row];
if([type isEqualToString:@"Money Donation"]) {
cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
[cell.descview setHidden:TRUE];
[cell.totalquantity setHidden:TRUE];
[cell.fulfilledquantity setHidden:TRUE];
return cell;
}
else if([type isEqualToString:@"Service"]) {
cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
cell.descview.text = (NSString *)[array2 objectAtIndex:indexPath.row];
[cell.totalquantity setHidden:TRUE];
[cell.fulfilledquantity setHidden:TRUE];
return cell;
}
cell.createdby.text = (NSString *)[array3 objectAtIndex:indexPath.row];
cell.product.text = (NSString *)[array4 objectAtIndex:indexPath.row];
cell.expiration.text = (NSString *)[array5 objectAtIndex:indexPath.row];
cell.totalquantity.text = (NSString *)[array6 objectAtIndex:indexPath.row];
cell.fulfilledquantity.text = (NSString *)[array7 objectAtIndex:indexPath.row];
cell.titleview.text = (NSString *)[array1 objectAtIndex:indexPath.row];
cell.descview.text = (NSString *)[array2 objectAtIndex:indexPath.row];
CGRect bodyFrame = cell.titleview.frame;
bodyFrame.size = cell.titleview.contentSize;
[cell.titleview setFrame:bodyFrame];
CGRect bodyFrame1 = cell.descview.frame;
bodyFrame1.size = cell.descview.contentSize;
[cell.descview setFrame:bodyFrame1];
CGRect bodyFrame2 = cell.createdby.frame;
[cell.createdby setFrame:bodyFrame2];
CGRect bodyFrame3 = cell.product.frame;
[cell.product setFrame:bodyFrame3];
CGRect bodyFrame4 = cell.expiration.frame;
[cell.expiration setFrame:bodyFrame4];
CGRect bodyFrame5 = cell.totalquantity.frame;
[cell.totalquantity setFrame:bodyFrame5];
CGRect bodyFrame6 = cell.fulfilledquantity.frame;
[cell.fulfilledquantity setFrame:bodyFrame6];
return cell;
}
when i tried this code my screen looks like this 当我尝试此代码时,我的屏幕看起来像这样
Please help me to Solve this issue 请帮我解决这个问题
Check this link - http://www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/ 检查此链接-http: //www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/
It will explain how to resize UIlabel to fit contents. 它将说明如何调整UIlabel的大小以适合内容。
One more suggestion would be use a single control and use NSFormatted Text instead of so many labels. 另一个建议是使用单个控件并使用NSFormatted Text而不是太多标签。 (Again it depends on your requirement).
(再次取决于您的要求)。
You need to move the labels' frame origin as well so that they are placed below each other. 您还需要移动标签的框架原点,以便将它们放置在彼此下方。 So after you calculate the first textView's size and bounds, set the origin of the second textView to whichever offset from the bottom of the first textView.
因此,在计算出第一个textView的大小和范围之后,将第二个textView的原点设置为与第一个textView底部的偏移量。 But that gets complicated when you have to take into account different orientations and device screen sizes...
但是,当您必须考虑不同的方向和设备屏幕尺寸时,这将变得很复杂。
Why don't you use auto-layout and constraints? 为什么不使用自动布局和约束?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.