2016-07-15 2 views
0

TableViewCell을 사용하고 웹 서비스 (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이 있고 라벨에 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가 또는 텍스트 뷰에 맞게 문자열에 필요한 높이를 계산할 수 있습니다 사용자 정의 셀입니다.

또한 문자열의 글꼴을 지정해야합니다. 즉. 문자열의 컨테이너에 사용 된 글꼴.

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

관련 문제