Iphone 在CellForRowIndexPath中,当单元格高度可变时,获取单元格高度
我在tableview中创建自定义单元格,有些单元格有图像,有些单元格很高,有些单元格只是文本。单元格的高度是以heightForRowAtIndexPath计算的,我相信这是在调用cellForRowAtIndexPath之前完成的。我想在单元格底部放置一个图像视图,而不管其高度如何,但我不确定如何从单元格内获取计算出的高度ForRowatineXpath?您可以自己调用HeightForRowatineXpath!只需将CellForRowatineXpath中的indexPath作为参数传递,就可以知道正在设置的单元格的高度 假设您使用的是UITableViewController,只需在CellForRowatineXpath中使用此命令即可Iphone 在CellForRowIndexPath中,当单元格高度可变时,获取单元格高度,iphone,ios,Iphone,Ios,我在tableview中创建自定义单元格,有些单元格有图像,有些单元格很高,有些单元格只是文本。单元格的高度是以heightForRowAtIndexPath计算的,我相信这是在调用cellForRowAtIndexPath之前完成的。我想在单元格底部放置一个图像视图,而不管其高度如何,但我不确定如何从单元格内获取计算出的高度ForRowatineXpath?您可以自己调用HeightForRowatineXpath!只需将CellForRowatineXpath中的indexPath作为参数传
float height = [self heightForRowAtIndexPath:indexPath]
你可以自己叫heightForRowAtIndexPath!只需将CellForRowatineXpath中的indexPath作为参数传递,就可以知道正在设置的单元格的高度 假设您使用的是UITableViewController,只需在CellForRowatineXpath中使用此命令即可
float height = [self heightForRowAtIndexPath:indexPath]
您可以询问代理,但您将询问两次,因为tableView已经询问并相应调整单元格大小。最好从细胞本身找到答案
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
您可以询问代理,但您将询问两次,因为tableView已经询问并相应调整单元格大小。最好从细胞本身找到答案
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
来不及回答了 但是,正如@user216661所指出的,获取单元格或ContentView的高度的问题在于它返回单元格的原始高度。对于高度可变的行,这是一个问题 更好的解决方案是获取单元格的Rect(
rectforrowatinexpath
),然后从中获取高度
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
来不及回答了 但是,正如@user216661所指出的,获取单元格或ContentView的高度的问题在于它返回单元格的原始高度。对于高度可变的行,这是一个问题 更好的解决方案是获取单元格的Rect(
rectforrowatinexpath
),然后从中获取高度
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
CGFloat height=[tableView.delegate tableView:tableView heightForRowAtIndexPath:indexath]代码>CGFloat height=[tableView.delegate tableView:tableView height for row索引路径:indexath]代码>这似乎不起作用,从cell.contentView.bounds.size.height返回的高度与在heightForRowAtIndexPathMark中计算的高度不同,您找到解决方案了吗?cell.contentView似乎正在返回单元格的原始高度和宽度。这似乎不起作用,从cell.contentView.bounds.size.height返回的高度与在heightForRowAtIndexPathMark中计算的高度不同,您找到解决方案了吗?cell.contentView似乎正在返回单元格的原始高度和宽度。