2013-03-25 109 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