uitableviewcell 自适应大小 参考
2015-10-13 20:10
495 查看
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何种字体
UIFont *font = [UIFont systemFontOfSize:13];
// 內容
NSString *content = [data objectAtIndex:indexPath.row];
// 计算显示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 返回需要的高度
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何种字体进行显示
UIFont *font = [UIFont systemFontOfSize:13];
//內容
NSString *content = [data objectAtIndex:indexPath.row];
// 计算显示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設置顯示榘形大小
rect.size = size;
// 重置列文本区域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 设置自动换行(重要)
cell.textLabel.numberOfLines = 0;
// 显示的字体(一定要和之前计算时使用的一至)
cell.textLabel.font = font;
return cell;
}
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何种字体
UIFont *font = [UIFont systemFontOfSize:13];
// 內容
NSString *content = [data objectAtIndex:indexPath.row];
// 计算显示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 返回需要的高度
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何种字体进行显示
UIFont *font = [UIFont systemFontOfSize:13];
//內容
NSString *content = [data objectAtIndex:indexPath.row];
// 计算显示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設置顯示榘形大小
rect.size = size;
// 重置列文本区域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 设置自动换行(重要)
cell.textLabel.numberOfLines = 0;
// 显示的字体(一定要和之前计算时使用的一至)
cell.textLabel.font = font;
return cell;
}
相关文章推荐
- POJ 3934 Queue(DP)
- Ant编译问题三:Build Falied ***resolve ro a path with no project.properties file for ***
- UE3 性能调试:动态光照环境
- 触发UIButton长按事件
- Android RelativeLayout 布局android:layout_centerHorizontal="true"注意
- 利用qemu-guest-agent冻结文件系统
- request
- 使用uitableview的索引功能
- UIView与CALayer的理解
- UIPickerView
- STL MAP(key,value)
- ios更改UITabBarController背景以及选中背景图片的方法
- Prime Query (ZOJ 3911 线段树)
- Prime Query (ZOJ 3911 线段树)
- URAL 1814 Continued Fraction 数学 矩阵乘法
- iOS8中的UIActionSheet添加UIDatePicker后,UIDatePicker不显示问题
- 通过xib文件创建和连接UIView
- Spark Master UI
- Circular Sequence
- iOS中 数据持久化 UI高级_17