2013-03-25 108 views
4

我已經創建了具有表格視圖的應用程序。在每個單元格中使用視圖和標籤。但是如果我在(!單元格)代碼中創建視圖和單元格,它會返回空單元格,如果我刪除(!單元格)條件,它將顯示數據但不會佔用動態高度。誰能幫幫我嗎。動態更改目標中單元格的高度C

- (void)viewDidLoad{ 
NSString *Path = [[NSBundle mainBundle] bundlePath]; 
NSString *DataPath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", LanguageFile]]; 

NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:DataPath]; 

self.reloadArray = [tempDict objectForKey:@"Rows"];} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return [self.reloadArray count]; 
} 

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
// Get data for the current row 
    NSString *textData = [reloadArray objectAtIndex:indexPath.section] 

CGFloat dataTextHeight = [self getLabelHeightForIndex:textData]; 

    if(dataTextHeight < 44) 
    { 
     dataTextHeight = 44; 
    } 

    return dataTextHeight; 
} 

-(CGFloat)getLabelHeightForIndex:(NSString *)string 
{ 
CGSize maximumSize = CGSizeMake(280, 10000); 

CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping]; 

if(labelHeightSize.height < 44){ 
    labelHeightSize.height = 44; 
} 

return labelHeightSize.height; 
} 



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

static NSString *CellIdentifier = @"Cell"; 

static const int textViewTag = 1, textLabelTag = 2; 

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"standard_back.png"]]; 

img.frame = tableView.frame; 
tableView.backgroundView = img; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (!cell) { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    // First view 
    UIView *textView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 280.0, 36.00)]; 
    textView.tag = textViewTag; 
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    [cell.contentView addSubview:textView]; 

    // First label 
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 270.0, 36.00)]; 
    textLabel.tag = textLabelTag; 
    textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f]; 
    textLabel.textColor = [UIColor whiteColor]; 
    textLabel.backgroundColor = [UIColor clearColor]; 
    textLabel.numberOfLines = 0; 
    textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    // textLabel.clipsToBounds = YES; 
    [cell.contentView addSubview:textLabel]; 
} 


    NSString *textData = [reloadArray objectAtIndex:(indexPath.section)]; 

    CGFloat dataTextHeight = [self getLabelHeightForIndex:textData]; 

    UIView *textView = [cell.contentView viewWithTag:textViewTag]; 
    CGRect textViewFrame = textView.frame; 
    textView.frame = CGRectMake(0.0, 0.0, textViewFrame.size.width, dataTextHeight); 

    UILabel *textLabel = [cell.contentView viewWithTag:textLabelTag]; 
    CGRect textLabelFrame = textLabel.frame; 
    textLabel.frame = CGRectMake(10.0, 0.0, textLabelFrame.size.width, dataTextHeight); 
    textLabel.text = textData; 
    textLabel.backgroundColor= [UIColor clearColor]; 
    textLabel.textAlignment = NSTextAlignmentCenter; 


cell.backgroundColor = [UIColor colorWithWhite:0 alpha:.65]; 
cell.textLabel.numberOfLines = 0; // Multiline 
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

return cell; 
} 

在此先感謝。

+0

cell.frame.size.height – 2013-03-25 07:30:19

+0

' - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath' – 2013-03-25 09:45:50

+0

可能重複[在iPhone中動態更改UITableviewCell高度?](http://stackoverflow.com/questions/8846374/change-uitableviewcell-height-dynamically-in-iphone) – hjpotter92 2013-03-26 08:08:51

回答

1

這是我在我的應用程序中使用的代碼的一部分。它適用於我。如果你需要幫助,請給我打電話。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
CGSize constraintSize = {230.0, 20000}; //230 is cell width & 20000 is max height for cell 
CGSize neededSize = [ [NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row]] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeCharacterWrap]; 


return MAX(45, neededSize.height +33); 


} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

{  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


} 


CGSize constraintSize = {230.0, 20000}; 


UILabel* label = [[UILabel alloc] init]; 

[label setNumberOfLines:0]; 

label.backgroundColor = [UIColor clearColor]; 

[label setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f]]; 

label.adjustsFontSizeToFitWidth = NO; 
CGSize neededSize = [ [NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row] ] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeCharacterWrap]; 
// NSLog(@"Height%f",neededSize.height); 
//NSLog(@"width%f",neededSize.width); 
[label setText:[NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row] ]]; 

[label setFrame:CGRectMake(10, 2, 230, MAX(neededSize.height+30, 44.0f))]; 

[[cell contentView] addSubview:label]; 




cell.selectionStyle=UITableViewCellSelectionStyleNone; 


return cell; 


} 

希望它有幫助。

+0

我正在使用xcode 4.6,其中autorelease和release命令已棄用。任何人都可以告訴我什麼是替代方法。 – 2013-03-25 11:32:30

+0

@NehaDangui只需刪除所有release/retain/autorelease調用。 – Motasim 2013-03-25 11:52:03

+0

@NehaDangui你在你的項目中使用ARC嗎? – Dany 2013-03-25 12:09:57

4

我看到很多解決方案,但都是錯誤的或不完整的。 您可以使用viewDidLoad和自動佈局中的5行來解決所有問題。 這對於objetive C:

_tableView.delegate = self; 
_tableView.dataSource = self; 
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout 
self.tableView.rowHeight = UITableViewAutomaticDimension; 
[self.tableView setNeedsLayout]; 
[self.tableView layoutIfNeeded]; 
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ; 

爲SWIFT 2.0:

self.tableView.estimatedRowHeight = 80 
self.tableView.rowHeight = UITableViewAutomaticDimension  
self.tableView.setNeedsLayout() 
self.tableView.layoutIfNeeded() 
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) 

現在創建廈門國際銀行或成的tableview在你的故事板 你的細胞有了這個,你不需要更多或重寫實現什麼。 (唐忘記數OS線0)和底部的標籤(限制)降級 「內容擁抱優先 - 垂直250」

enter image description here

您可以在接下來的URL donwload代碼: https://github.com/jposes22/exampleTableCellCustomHeight

參考文獻:http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return "your content height"; 
} 
+0

請編輯更多信息。僅限代碼和「嘗試這個」的答案是不鼓勵的,因爲它們不包含可搜索的內容,也不解釋爲什麼有人應該「嘗試這個」。我們在這裏努力成爲知識的資源。 – Rick 2016-07-19 13:29:58