![](/img/trans.png)
[英]Xamarin ios how to make a self-sizing UITableViewCell while editing
[英]IOS:Creating a Self-Sizing UITextView Within a UITableViewCell?
我正在為項目使用滑板。 我的項目基於解析json數據並將其顯示為JSON。 解析JSON時,我將收到兩個不同的數據。 Title and Description
我必須解析它,並且必須在UItextview inside a uitableviewcell
修復,我已經像這樣構造了uitableviewcell
TEXTVIEW,TEXTVIEW,LABEL,LABEL,LABEL,LABEL,LABEL; 現在我的問題是我的文本視圖必須根據數據調整大小,並且基於該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;
}
當我嘗試此代碼時,我的屏幕看起來像這樣
請幫我解決這個問題
檢查此鏈接-http: //www.springtiger.co.uk/2010/11/28/dynamically-generate-labels-frame-to-match-its-content/
它將說明如何調整UIlabel的大小以適合內容。
另一個建議是使用單個控件並使用NSFormatted Text而不是太多標簽。 (再次取決於您的要求)。
您還需要移動標簽的框架原點,以便將它們放置在彼此下方。 因此,在計算出第一個textView的大小和范圍之后,將第二個textView的原點設置為與第一個textView底部的偏移量。 但是,當您必須考慮不同的方向和設備屏幕尺寸時,這將變得很復雜。
為什么不使用自動布局和約束?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.