Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为UITableView的不同行设置不同的高度_Ios_Objective C_Uitableview_Uitextview - Fatal编程技术网

Ios 如何为UITableView的不同行设置不同的高度

Ios 如何为UITableView的不同行设置不同的高度,ios,objective-c,uitableview,uitextview,Ios,Objective C,Uitableview,Uitextview,如何在ios中为自定义ui表视图单元格的不同行设置不同的高度? 我正在尝试根据自定义uitableview单元格内的uitextview中有多少行来更改高度 我尝试在heightForRowAtIndexPath方法中设置这样的高度,但它崩溃了: PostStreamCell *cell = [tableView cellForRowAtIndexPath:indexPath]; int lines = cell.txtViewMessage.contentSize.height / cell.

如何在ios中为自定义ui表视图单元格的不同行设置不同的高度? 我正在尝试根据自定义uitableview单元格内的uitextview中有多少行来更改高度

我尝试在heightForRowAtIndexPath方法中设置这样的高度,但它崩溃了:

PostStreamCell *cell = [tableView cellForRowAtIndexPath:indexPath];
int lines = cell.txtViewMessage.contentSize.height / cell.txtViewMessage.font.lineHeight;

if(lines < 4)
{
    return 100;
}
else if(lines == 4)
{
    return 100;
}
else{
    return 220;
}
PostStreamCell*cell=[tableView cellforrowatinexpath:indexPath];
int lines=cell.txtViewMessage.contentSize.height/cell.txtViewMessage.font.lineHeight;
如果(行数<4)
{
返回100;
}
else if(行==4)
{
返回100;
}
否则{
返回220;
}

尝试使用
文本边界矩形大小..
计算单元格本身的大小

大概是这样的:

 NSString *text = yourTextView.text;
    CGSize size;

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                          nil];
    CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(525, 500)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attributesDictionary
                                             context:nil];
    size = fontSizeFor7.size;

    return size.height +40;

无法在
高度FORROWATINDEXPATH
中使用
单元格FORROWATINDEXPATH
,因为该单元格不存在。您必须用另一种方式检索文本,而不使用单元格。

我建议您数一数 内线 在里面 viewDidLoad 方法使用文本字段的长度。然后将其存储在某个数组中,并在 ROWATINDEX路径的高度 方法。应该是这样的

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (myLinesArray[indexPath.row] =< 4)
{
    return 100;
}
else return 220;
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
if(myLinesArray[indexPath.row]=<4)
{
返回100;
}
否则返回220;
}

您正在正确设置高度——但是,您可能不应该通过调用
cellForRowatineXpath
来检索
HeightForRowatineXpath
中的单元格,除非您的单元格已经静态创建。
heightforrowatinexpath
(以下)的基本实现不会崩溃,因此问题可能存在于视图控制器实现的其他地方或其他地方。 还注意到,您正在除以一个动态值以获得高度。检查PostStreamCell——如果单元格对象
txtViewMessage
或其
font
属性为nil,则将除以0,这可能会导致崩溃。 //Very simple implementation to demo changes in height - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 2) { return 60; } else { return 44; } }

//演示高度变化的非常简单的实现 -(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径 { 如果(indexath.row%2){ 返回60; }否则{ 返回44; } }
坠机消息是什么?实际上不要告诉我,可能是因为你被零除了?表格视图单元格是在heightForRorAtIndexPath之后创建的:那么您的单元格对象也将为零?添加断点并查看单元格是否为nil。否该单元格不可能为nil,但我的uitextview位于自定义uitable视图单元格中。如何访问该单元格并获取该uitextview的大小?好的,为什么您不能访问自定义单元格中的uitextview,该自定义单元格位于
heightForRowAtIndexPath
委托中。如果我这样做,则只会选择indexpath处的第一行请参见有很多方法,您可以在UItableView之前的文本数组中保存,或者对每一行相应地处理条件