実際には、UITableView
の高さが動的に正しく設定されています。私はAutoLayoutを使用していません。テーブルがロードされたときに初めて、セルの上下に多くのスペースが含まれていて、一部の時間イメージは他のセルと重なっています。私は画像のために細胞の正しい高さを計算することができません(画像は高さが大きくなります)以下はコードです。Swiftを使用し、AutoLayoutを使用しないで、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(高さが長すぎる可能性があります)が含まれています。以下は、セルのコードです。
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)
}
}
}
Autolayoutを使用すると、動的イメージの高さに基づいて行の高さを手動で計算する必要はありません。 –
@DávidPásztorで手の高さを手動で計算することはできますか? –
画像の高さを管理する場合は、はい。しかし、Autolayoutの使用を強くお勧めします。これは、できるだけ早く使用することを覚えておく必要がある、iOS UIデザインの本当に重要な機能であるためです。 –