动态改变目标C中的单元格高度

时间:2013-03-25 07:28:16

标签: ios objective-c uitableview

我创建了一个带有表格视图的应用。它使用每个单元格中的视图和标签。但是如果我在(!单元格)代码中创建视图和单元格,它将返回空单元格,如果我删除(!单元格)条件,它将显示数据但不采用动态高度。任何人都可以帮助我。

- (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;
}

提前致谢。

3 个答案:

答案 0 :(得分:4)

我看到了很多解决方案,但都是错误的或不完整的。 您可以在viewDidLoad和autolayout中解决5行的所有问题。 这对于客观的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)

现在使用xib创建您的单元格或在Storyboard中创建tableview 有了这个你不需要实现任何更多或覆盖。 (不要忘记数字os行0)和底部标签(约束)降级&#34;内容拥抱优先级 - 垂直到250&#34;

enter image description here

您可以在下一个网址中下载代码: https://github.com/jposes22/exampleTableCellCustomHeight

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

答案 1 :(得分:2)

这是我在我的应用中使用的代码的一部分。它对我很有用。如果你需要帮助,请给我打电话。

- (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;


}

希望它有所帮助。!!!

答案 2 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return "your content height";
}
相关问题