1

第 1 步 - 这工作正常

我有加载自定义 UITableViewCells 的 UITableView

第 2 步 - 这有点用

我更改了 UITableViewCell 高度,以便 UITextView 中单元格中包含的所有数据都可见

我设法获取 UItextView 数据并使用以下代码调整其大小:

UITextView *dummy = [[UITextView alloc] init];
dummy.font = [UIFont systemFontOfSize:14];
dummy.text = cell.textView.text;

CGSize newSize = [dummy sizeThatFits:CGSizeMake(270.0f, 500.0f)];

//get the textView frame and change it's size
CGRect newFrame = cell.textView.frame;
newFrame.size = CGSizeMake(270.0f, fmaxf(newSize.height, 60));
cell.textView.frame = newFrame;

//resize the cell view I add +95 because my cell has borders and other stuff...
CGRect cellFrame = CGRectMake(0, 0, 320, newFrame.size.height+95);
cell.cellView.frame = cellFrame;

然后我设法使用委托函数 heightForRowAtIndexPath 设置 UITableViewCell 高度:

现在,当我运行代码并上下滚动表格单元格时,行为并不总是如预期的那样......即单元格大小并不总是正确的大小,但如果我上下滚动它有时会加载到右边再次大小。我在想也许 dequeueReusableCellWithIdentifier 是问题所在

cellIdentifier = @"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

由于我正在回收具有相同标识符的不同尺寸的电池...

任何人都可以给我一些关于如何最好地解决这个问题的指导吗?

谢谢!!!

首次加载单元格时的屏幕截图1

屏幕截图 2 滚动后注意顶部单元格如何被纠正

4

2 回答 2

0

我将从在 cellForRowAtIndexPath 方法中动态添加 UITextView 开始。假设(数据数组包含要在单元格内显示的内容)//定义

#define CELL_TEXTVIEW_WIDTH = 320        
#define CELL_TEXTVIEW_HEIGHT = 9999  
#define PADDING = 5

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"tableCell";

ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if(cell == nil){

cell =  [[tableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

 NSString *_data = [data objectAtIndex:indexPath.row];

CGSize _data_contentsize  = [_data sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(CELL_TEXTVIEW_WIDTH, CELL_TEXTVIEW_HEIGHT) lineBreakMode:NSLineBreakModeWordWrap];

UITextView *dummy=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, _data_contentsize +(PADDING * 2))];

// add Font and text color for your textview here

[cell.contentView addSubview:dummy];

return cell;

}

在此之后计算 heightForRowAtIndexPath 方法下的单元格的高度。

于 2013-10-24T18:02:18.497 回答
0

您是否实施了以下方法?

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath

你应该为每一行设置高度,如果它们彼此不同

于 2013-10-24T15:00:31.127 回答