ios UITableView高度自适应(转)
2013-09-22 10:43
190 查看
- (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]; // 構建顯示行 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; }
相关文章推荐
- IOS UItableview UIcollectionview cell高度自适应
- iOS开发之UITableview之多种Cell高度自适应实现方案的UI流畅度分析
- iOS UITableView cell自适应内容高度
- 【iOS开发】UITableView Cell 自适应高度
- iOS UITableView+FDTemplateLayoutCell 配合AutoLayout分分钟教你实现动态高度自适应
- IOS UItableview UIcollectionview cell高度自适应
- iOS开发继iOS8后UITableView的Cell高度自适应
- 【iOS】Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」
- (原创)iOS中cell高度自适应方法UITableView设置
- ios webview自适应实际内容高度4种方法
- iOS开发之解决WebView自适应内容高度
- 计算在UITableView中的UICollectionView的高度(自适应)(iOS10可用)
- 支持android&ios高度自适应及JS相互调用的组件react-native-webview2
- UItableView cell 自适应高度
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- iOS textView 高度自适应
- IOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS【野路子】精准获取webView内容高度,自适应高度
- UItableView cell 自适应高度