मैं इसमें एक मल्टीलाइन लेबल के साथ एक टेबल व्यू करने की कोशिश कर रहा हूं। दुर्भाग्य से, मेरे पास इसके साथ कोई भाग्य नहीं था - कोशिकाओं को बिल्कुल भी आकार नहीं देता, हालांकि मुझे मल्टीलाइन पर लेबल मिलता है।
TableViewController का मेरा स्विफ्ट कोड:
import UIKit
class QuestionsController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questionsData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell", forIndexPath: indexPath) as! BasicQuestionCell
let question = questionsData[indexPath.row] as Question
cell.titleLabel.text = question.content
return cell
}
}
परिणाम:
उत्तर:
जवाब के लिए 5 № 1जैसा कि आपने उल्लेख नहीं किया है कि यदि आप ऑटो लेआउट का उपयोग कर रहे हैं या नहीं, तो मैं यह मान रहा हूं कि आप ऑटो लेआउट का उपयोग कर रहे हैं और सेल के अंदर लेबल के लिए आवश्यक बाधाएं दी हैं।
अब बस हटाना दो लाइनों के बाद व्यूडीडलड () से:
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
तथा जोड़ना the डिफ़ॉल्ट फ़ंक्शन इसे आगे बढ़ाने के लिए इस प्रकार है:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.rowHeight
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
परिणाम:
जवाब के लिए 2 № 2
यूआईसेलव्यू सेट करने के लिए कई चीजें हैं जो इसे "एस लेबल" की ऊंचाई से मेल खाती हैं।यदि आप सटीक ऊंचाई जानते हैं तो आप बस लागू कर सकते हैं:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// return the height there
return 100.0;
}
यदि आप "टी ऊंचाई पता नहीं है, तो आप इसे चलाने के समय में ऊंचाई प्राप्त करने की जरूरत है, और एक ही समारोह में reuturn ।
आपकी कोशिकाओं को कैसे संरचित किया जाता है, इसको परिभाषित करने के कई तरीके हैं।मैं आपको मान रहा हूं "गतिशील प्रोटोटाइप का उपयोग कर रहे हैं और बस बुनियादी शैली कोशिकाओं का उपयोग कर रहे हैं।
उपरोक्त विधि में आप कर सकते हैं:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if let hieght = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.frame.height {
return hieght;
} else {
print("Couln"t adjust the height")
return 100.0;
}
}
यदि आप "आपको अलग-अलग तरीके से सेल"s को फिर से परिभाषित करते हैं, तो आप "डी बस एक ही विधि को लागू करते हैं, लेकिन सेल के भीतर अपने लेबल की ऊंचाई वापस करते हैं।