gpt4 book ai didi

ios - 使用 Swift 且不使用 AutoLayout 时,UITableView 高度有时动态不正确

转载 作者:行者123 更新时间:2023-11-30 12:16:10 28 4
gpt4 key购买 nike

确实,我有一个关于设置 UITableView 高度动态正确的问题。我没有使用自动布局。第一次加载表格时,它包含单元格顶部和底部的大量空间,有时图像与其他单元格重叠。由于图像(图像的高度可能很大),我无法计算单元格的正确高度,下面是代码。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return cellHeight
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell:Templategt4 = tableView.dequeueReusableCell(withIdentifier: "Templategt4Identify", for: indexPath) as! Templategt4
cell.delegate = self;
cell.configure( self.storyData[0], row: indexPath, screenSize: screenSize,
gt4tableview: self.storyTableView);
//self.secondWebViewLable.scrollView.contentSize.height
cellHeight = cell.firstWebViewLable.frame.height + cell.firstImage.frame.height + cell.secondWebViewLable.frame.height;

//cellHeight = cellHeight + 200

print("cellForRowAt cellHeight", cellHeight);

return cell
}

我正在使用自定义单元格。该单元格包含 UiWebView、UIImageView(高度可能太长)。下面是cell的代码。

class Templategt4: UITableViewCell, UIWebViewDelegate{

@IBOutlet weak var firstWebViewLable: UIWebView!
@IBOutlet weak var firstImage: UIImageView!
@IBOutlet weak var secondWebViewLable: UIWebView!
@IBOutlet weak var playiconimage: UIImageView!
let padding = 24;
var contentHeights1 : [CGFloat] = [0.0]
var contentHeights2 : [CGFloat] = [0.0]
var imageHeights : [CGFloat] = [0.0]
var gt4tableview: UITableView? = nil
var indexpath: IndexPath? = nil
var delegate:ImageClickedForStoryDetails?

class MyTapGestureRecognizer: UITapGestureRecognizer {
var youTubeCode: String?
}

func configure(_ data: StoryDetailsRequest, row: IndexPath, screenSize: CGRect, gt4tableview: UITableView) {
self.gt4tableview = gt4tableview;
self.indexpath = row;
let callData = data.content[(row as NSIndexPath).row];
let url = Constants.TEMP_IMAGE_API_URL + callData.image;

// this is first webview data.
self.firstWebViewLable.frame.size.height = 1
self.firstWebViewLable.frame.size = firstWebViewLable.sizeThatFits(.zero)
let htmlString1:String! = callData.text1
self.firstWebViewLable.delegate = self;
self.firstWebViewLable.loadHTMLString(htmlString1, baseURL: nil)
// this is second webview data.

self.secondWebViewLable.frame.size.height = 1
self.secondWebViewLable.frame.size = secondWebViewLable.sizeThatFits(.zero)
let htmlString2:String! = callData.text2
self.secondWebViewLable.loadHTMLString(htmlString2, baseURL: nil)
self.secondWebViewLable.delegate = self;

if( !callData.image.isEmpty ) {

let range = url.range(of: "?", options: .backwards)?.lowerBound
var u:URL!
if(url.contains("?")) {
u = URL(string: url.substring(to: range!))
} else {
u = URL(string: url)
}
self.firstImage.image = nil
self.firstImage.kf.setImage(with: u, placeholder: nil,
options: nil, progressBlock: nil, completionHandler: { image, error, cacheType, imageURL in
if( image != nil) {
let imageW = (image?.size.width)!;
let imageheight = (image?.size.height)!;

self.firstImage.image = image;
self.firstImage.contentMode = UIViewContentMode.scaleAspectFit
self.firstImage.clipsToBounds = false

let ratio = self.firstImage.frame.size.width/imageW;

let scaledHeight = imageheight * ratio;
if(scaledHeight < imageheight)
{
//update height of your imageView frame with scaledHeight
self.firstImage.frame = CGRect(x: 0,
y: Int(self.firstWebViewLable.frame.origin.y + self.firstWebViewLable.frame.height),
width: Int(screenSize.width)-self.padding,
height: Int(scaledHeight) )

} else {
self.firstImage.frame = CGRect(x: 0,
y: Int(self.firstWebViewLable.frame.origin.y + self.firstWebViewLable.frame.height),
width: Int(screenSize.width)-self.padding,
height: Int(imageheight) )
}
} else {
self.firstImage.image = UIImage(named: "defaultimg")
}


self.secondWebViewLable.frame = CGRect(x: 0,
y: Int(self.firstImage.frame.origin.y + self.firstImage.frame.height),
width: Int(screenSize.width)-self.padding,
height: Int(self.secondWebViewLable.frame.height) )

if (self.imageHeights[0] == 0.0)
{
self.imageHeights[0] = self.firstImage.frame.height

UIView.performWithoutAnimation {
let ipath = IndexPath(item: (self.indexpath?.row)!, section: (self.indexpath?.section)!)
self.gt4tableview?.reloadRows(at: [ipath], with: UITableViewRowAnimation.none)
}
}


})




}

}

func webViewDidStartLoad(_ webView: UIWebView)
{
//myActivityIndicator.startAnimating()
}

func webViewDidFinishLoad(_ webView: UIWebView)
{
if(webView == self.firstWebViewLable) {

if (contentHeights1[0] != 0.0)
{
// we already know height, no need to reload cell
return
}
contentHeights1[0] = self.firstWebViewLable.scrollView.contentSize.height
UIView.performWithoutAnimation {
let ipath = IndexPath(item: (self.indexpath?.row)!, section: (self.indexpath?.section)!)
self.gt4tableview?.reloadRows(at: [ipath], with: UITableViewRowAnimation.none)
}


} else if(webView == self.secondWebViewLable) {

if (contentHeights2[0] != 0.0)
{
// we already know height, no need to reload cell
return
}

contentHeights2[0] = self.secondWebViewLable.scrollView.contentSize.height
UIView.performWithoutAnimation {
let ipath = IndexPath(item: (self.indexpath?.row)!, section: (self.indexpath?.section)!)
self.gt4tableview?.reloadRows(at: [ipath], with:UITableViewRowAnimation.none)
}

}

}

最佳答案

您不应在 cellForRow 方法中计算单元高度。并使用此方法代替 heightForRowAt:

TableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat

并在您的viewDidLoad中调用它。

tableView.rowHeight = UITableViewAutomaticDimension

关于ios - 使用 Swift 且不使用 AutoLayout 时,UITableView 高度有时动态不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45411643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com