zoukankan      html  css  js  c++  java
  • 动态调整UITableViewCell高度的实现方法

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     
        static NSString *CellIdentifier = @"Cell";
     
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    		UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    		label.tag = 1;
    		label.lineBreakMode = UILineBreakModeWordWrap;
    		label.highlightedTextColor = [UIColor whiteColor];
    		label.numberOfLines = 0;
    		label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
    		label.backgroundColor = [UIColor clearColor];
    		[cell.contentView addSubview:label];
    		[label release];
        }
     
        UILabel *label = (UILabel *)[cell viewWithTag:1];
    	NSString *text;
    	text = [textArray objectAtIndex:indexPath.row];
        CGRect cellFrame = [cell frame];
    	cellFrame.origin = CGPointMake(0, 0);
     
    	label.text = text;
    	CGRect rect = CGRectInset(cellFrame, 2, 2);
    	label.frame = rect;
    	[label sizeToFit];
    	if (label.frame.size.height > 46) {
    		cellFrame.size.height = 50 + label.frame.size.height - 46;
    	}
    	else {
    		cellFrame.size.height = 50;
    	}
    	[cell setFrame:cellFrame];
     
        return cell;
    }
     
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    	UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
            //UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
    	return cell.frame.size.height;
    }

    本文转载至 http://longtimenoc.com/archives/动态调整uitableviewcell高度的实现方法
  • 相关阅读:
    WebService是什么?以及工作原理
    分布锁的问题?
    反射是什么?原理?作用?
    HTTP/1.1与HTTP/1.0的区别
    Ajax的跨域问题(包括解决方案)?
    SVN与Git优缺点比较
    类的加载过程?
    B树, B-树,B+树,和B*树的区别
    Linux常用的50个命令
    权限模型
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3152093.html
Copyright © 2011-2022 走看看