मैं सेल की ऊंचाई को गतिशील रूप से बदलना चाहता हूं, ताकि छवि छवि को चौड़ाई में भर दे और छवि पहलू अनुपात को बनाए रखे।
मैं इस का उपयोग करने की कोशिश कर रहा हूँ UITableViewAutomaticDimension
। ऑनलाइन उपलब्ध ट्यूटोरियल के आधार पर, यहएक सीधा काम लग रहा था। लेकिन मेरे मामले में, सेल की ऊँचाई इस कार्य को करने के लिए समायोजित नहीं होती है। या तो छवि पहलू अनुपात से भर जाती है, जिससे बहुत अधिक चौड़ाई का स्थान खाली हो जाता है या चौड़ाई भर जाती है, लेकिन छवि के हिस्से को लंबवत रूप से क्लिप करता है। सेल दृश्य के संबंध में अग्रणी, अनुगामी, शीर्ष और नीचे की बाधाएं हैं और यह भी tableView.rowHeight = UITableViewAutomaticDimension
और table.estimatedRowHeight को viewDidLoad में सेट किया गया है। मैंने इस लिंक पर प्रोजेक्ट अपलोड किया है। क्या कोई सुझाव दे सकता है कि मैं यहां क्या याद कर रहा हूं?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var picTableView: UITableView!
let reuseidentifier = "dynamicCellTableViewCell"
let images = [UIImage(named: "feedImage1"), UIImage(named: "feedImage2"), UIImage(named: "feedImage3")]
override func viewDidLoad() {
super.viewDidLoad()
picTableView.rowHeight = UITableViewAutomaticDimension
picTableView.estimatedRowHeight = 1000
}
// MARK: Table View Data Source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return images.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseidentifier, forIndexPath: indexPath) as! DynamicCellTableViewCell
cell.displayImageView.image = images[indexPath.row]
return cell
}
}
उत्तर:
जवाब के लिए 2 № 1मैंने हल निकाला।UIImageView जब UITableViewAutomaticDimension के साथ उपयोग किया जाता है, केवल UIImage की वास्तविक चौड़ाई और आकार के रूप में ऊँचाई की तलाश करता है। इसलिए UIImageView, UIImage "मूल ऊँचाई" पर प्रतिसाद देने के लिए इसकी ऊँचाई को समायोजित करता है न कि स्केल की गई चौड़ाई के लिए स्केल की गई ऊँचाई (पहलू-रेखा) से लेआउट अड़चन का मिलान करने के लिए।
तो चाल पहले चौड़ाई और ऊंचाई को बदलने के लिए हैछवि की चौड़ाई और ऊँचाई से मेल खाने के लिए जो आपको सेल में चाहिए। बाकी काम UITableViewAutomaticDimension द्वारा किया जाएगा। नीचे अपडेटेड सेलफ़ोररॉइट इंडेक्स फ़ंक्शन है जो अब काम करता है। अपना इनपुट देने के लिए सभी का धन्यवाद।
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseidentifier, forIndexPath: indexPath) as! DynamicCellTableViewCell
let image = images[indexPath.row]!
let newWidth = cell.displayImageView.frame.width
let scale = newWidth/image.size.width
let newHeight = image.size.height * scale
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
cell.displayImageView.image = newImage
return cell
}
जवाब के लिए 0 № 2
आपको UITableViewController के लिए अगली विधि लागू करने की आवश्यकता है:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 1000.0;
}
विधि के अंदर, आपको पता होना चाहिए कि प्रत्येक सेल में क्या आयाम होगा। आपको पहले से पता होना चाहिए कि प्रत्येक सेल की ऊंचाई कितनी होगी।
उदाहरण के लिए, आप एक सरणी बना सकते हैं CGFloat की बचत से ऊंचाई की प्रत्येक पंक्ति होगी, और जब ऊँचाई बदलती है, तो आप इस स्थिति को सरणी में अपडेट कर सकते हैं और tableView.reloadData () को निष्पादित कर सकते हैं।
var arrayHeight:CGFloat?
//Now fill array with height of each cell
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return arrayHeight[indexPath.row];
}
जवाब के लिए 0 № 3
सेल।
सेल
CellForRowAtIndexPath प्रतिनिधि विधि में इन दो पंक्तियों को जोड़ें
और सुनिश्चित करें कि स्टोरीबोर्ड में शीर्ष, नीचे, दाएं, बाएं, बाधाएं सेट की गई हैं