2016-07-15 8 views
0

私はTableViewCellを持ち、Webサービス(JSON)でデータをロードします。データは異なる配列に格納され、データはtableviewにロードされます。私は、データサイズをある程度大きく、時間を小さく並べる必要があります。ラベルサイズをどのように管理するのか。私は例を試みたが、ダイナミックラベルを作っていない。助けてください、ありがとう。UITableViewCellで動的ラベルサイズを設定する方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [title count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //NSLog(@"tableview cell"); 
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"event"]; 
    if (cell==nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [img objectAtIndex:indexPath.row]]]; 
    UIImage* image = [[UIImage alloc] initWithData:imageData]; 
    cell.img.image =image; 

    cell.lbl_1.text=[NSString stringWithFormat:@"%@",[title objectAtIndex:indexPath.row]]; 

    cell.lbl_2.text=[NSString stringWithFormat:@"%@",[des objectAtIndex:indexPath.row]]; 
    cell.lbl_3.text=[NSString stringWithFormat:@"%@",[evnt_ary objectAtIndex:indexPath.row]]; 

    return cell; 
} 
+0

重複 - http://stackoverflow.com/q/13190425/3515115 –

+1

http://www.appcoda.com/self-sizing-cells/ –

+0

@balkaransinghお待ちください私はこのリンクを参照してください –

答えて

1

UITableViewAutomaticDimensionを確認してくださいと同じように使用します。

tableView.estimatedRowHeight = 44.0 
tableView.rowHeight = UITableViewAutomaticDimension 

あなたの制約が適切であることを確認してください。

+0

ViewDidLoadで試したが動作しなかった –

+0

はいの場合はheightforRowAtIndexpathを使用しました。その後、UITableViewAutomaticDimensionを返します。 –

+0

あなたのラベルにnumberOfLines = 0があり、LabelにLineBrakingModeがnslinebreakbywordwrapになっていることを確認してください –

0

まず、tableViewにカスタムセルを使用します。 tableViewで直接ラベルを使用すると、サイズを動的に変更すると読み込みの問題が発生します。

"cellForRowAtIndexPath"では、プロパティを変更できます。たとえば、「テーブルセルは、」カスタムセルであるならば、

TableCell *cell = (NBEServicesCell*)[tableView dequeueReusableCellWithIdentifier:"@yourIdentifier" forIndexPath:indexPath]; 

    //Assigning each row a number. 
[cell.yourLabel setFont:[UIFont fontWithName:@"Avenir-Heavy" size:14.0]]; 
cell.yourLabel.numberOfLines = 0; 
cell.yourLabel.lineBreakMode = NSLineBreakByWordWrapping; 
[cell.yourLabel sizeToFit]; 
cell.yourlabel.frame.size.width = newWidth; 
cell.yourlabel.frame.size.height = newHeight; 
+0

newWidthとnewHeightを書くには? –

+0

CGFloat newWidth = 100.0 – WasimSafdar

+0

あなたの問題を解決したら、私の答えを投票してください:) – WasimSafdar

0
CGSize maximumSize = CGSizeMake(width_specified, CGFLOAT_MAX); 


CGSize myStringSize = [myString sizeWithFont:myFont 
          constrainedToSize:maximumSize 
           lineBreakMode:self.myLabel.lineBreakMode]; 

あなたは所定の幅のためにlableかのTextViewに収まるように文字列で必要とされる高さを計算することができます。

文字列のフォントも指定する必要があります。すなわち、文字列のコンテナに使用されるフォント。

2

あなたのラベルにテキストを割り当てた後、[yourCell.yourlabel sizeToFit]と書いてください。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    yourCell.yourlabel.text = @"text whatever you want to set"; 
    [yourCell.yourlabel sizeToFit]; 
} 

またyourCell.yourlabelにテキストを割り当て、heightForRowAtIndexPathで[yourCell.yourlabel sizeToFit]を書き込みます。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    yourCustomCell *yourCellObj = (yourCustomCell *)[self tableView:self.tableViewObj cellForRowAtIndexPath:indexPath]; 

    yourCellObj.yourlabel.text = @"text whatever you want to set"; 
    [yourCellObj.yourlabel sizeToFit]; 

    return (yourCellObj.yourlabel.frame.origin.y + yourCellObj.yourlabel.frame.size.height); 
} 
+0

このコードでは、両方の行を使用しないでください。_table_view.estimatedRowHeight = 100.0; // _table_view.rowHeight = UITableViewAutomaticDimension ;? –

+0

私は確信していませんが、私はestimatedRowHeightを試したときに時々間違った出力を得ました。 – iHP

+0

** yourCellObj.numberOfLines = 0 ** @A.Kumawatを設定してください – iHP

0

あなたは、あなたがちょうどあなたのラベルのパラメータを渡すことがCGSizeを返します、次の機能を使用できるように、ラベルの内容(テキスト)に応じて、動的な幅&高さを取得する必要があります。

- (CGSize) messageSizeExact:(UILabel *) msgLabel 
{ 

    CGRect textRect = [msgLabel.text boundingRectWithSize:(CGSize){280.0, CGFLOAT_MAX} 
              options:NSStringDrawingUsesLineFragmentOrigin 
             attributes:@{NSFontAttributeName:msgLabel.font} 
              context:nil]; 

    return textRect.size; 
} 
0

PLZテーブルの出口を取る

@property (strong, nonatomic) IBOutlet UITableView *view_tableView; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view_tableView.estimatedRowHeight = 50; 
    self.view_tableView.rowHeight = UITableViewAutomaticDimension; 
    // Do any additional setup after loading the view, typically from a nib. 
} 


#pragma mark - TableView Delegate And DataSource 



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 


    return 1; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"]; 
    UILabel *label =(UILabel *)[cell viewWithTag:5000]; 

    [email protected]"Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.2016-07-15 15:22:28.819 Time'nTask[257:14278] Notification settings registered2016-07-15 15:22:28.820 Time'nTask[257:14278] Successfully registered for remote notifications With device Token 68048cc293d695fd3220cf63da3baa685675126ecd72af0547b760ee31571b62"; 

    return cell; 
} 

ラインのrember組ラベル番号= 0

enter image description here

関連する問題