2016-08-31 24 views
0

我的問題是如何調整UITableViewCell中的大圖像和固定大小的圖像。
根據像WhatsApp等數據,我需要單元格爲autoresize。我該怎麼做。 當我使用UILabel時,我的單元格正在調整大小,但是在圖像的情況下會將所有內容搞亂。根據UILabel自動調整單元格文字和圖像,如聊天

請給我建議。提前致謝。

@ddb這是我使用的源代碼。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
} 

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *[email protected]"admincell"; 
    adminCell =[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (adminCell==nil) { 
     adminCell=[[AdminPostTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
    } 
    adminCell.selectionStyle=UITableViewCellSelectionStyleNone; 
    [adminCell setBackgroundColor:[UIColor clearColor]]; 
    NSMutableDictionary *getAdminDataDictionary=[adminDataArray objectAtIndex:indexPath.row]; 
    if ([getAdminDataDictionary objectForKey:@"TEXT"]) 
    { 
     adminCell.lblSenderAdminPage.text=[getAdminDataDictionary objectForKey:@"TEXT"]; 
     adminCell.lblSenderAdminPage.textColor=[UIColor blackColor]; 
     adminCell.lblSenderAdminPage.backgroundColor=[UIColor whiteColor]; 
     adminCell.lblSenderAdminPage.preferredMaxLayoutWidth=305; 
     adminCell.lblSenderAdminPage.layer.masksToBounds=YES; 
     adminCell.lblSenderAdminPage.layer.cornerRadius=5; 
     adminCell.lblSenderAdminPage.layer.borderWidth=1.0f; 
     adminCell.lblSenderAdminPage.layer.borderColor=[UIColor blackColor].CGColor; 
    } 
    else if ([getAdminDataDictionary objectForKey:@"IMAGE"]){   
     adminCell.imgSenderAdminPage.image=[getAdminDataDictionary objectForKey:@"IMAGE"]; 
    } 
    return adminCell; 
} 
+0

分享自己當前的源代碼,請 – ddb

+0

試試這個鏈接http://stackoverflow.com/a/14841373/4970453 –

回答

0

您可以嘗試使用

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    CGSize messageSize // Your message size 
    if (img) { 
     CGFloat imgHeight = img.size.height; // Image size 
     return messageSize.height + imgHeight + 2* textMarginVertical; 
    } else { 
     return messageSize.height + 2* textMarginVertical; 
    } 
} 

在類的頂部聲明

static CGFloat textMarginVertical = 7f; 

編碼愉快..

+0

什麼是「img」? –

+0

你想要在單元格中設置的圖像文件.. –

+0

但是,當我將尺寸設置爲: - –

相關問題