6

नहीं दिखा रहा है मैं ऑटोलायआउट के साथ सेक्शन हेडर बनाने की कोशिश कर रहा हूं। एक शीर्षक और एक काउंटरUITableView अनुभाग शीर्षलेख

class ProfilePeopleListHeaderViewCell: UIView { 

    let titleLeftOffset = 12 
    let counterRightOffset = 5 
    let counterWidth = 50 

    var title = "title" { 
    didSet { 
     titleLabel.text = title 
    } 
    } 
    var numberOfPeople = 0 { 
    didSet { 
     peopleCounter.text = "\(numberOfPeople)" 
    } 
    } 

    let titleLabel = UILabel() 
    let peopleCounter = UILabel() 


    convenience init(title: String, numberOfPeople:Int) { 
    self.init() 
    self.title = title 
    self.numberOfPeople = numberOfPeople 
    backgroundColor = UIColor.greenColor() 
    titleLabel.textColor = Constants.Colors.ThemeGreen 
    titleLabel.textAlignment = .Left 
    if #available(iOS 8.2, *) { 
     titleLabel.font = Constants.Fonts.Profile.PeopleListHeaderFont 
     peopleCounter.font = Constants.Fonts.Profile.PeopleListHeaderFont 
    } else { 
     titleLabel.font = UIFont.systemFontOfSize(14) 
     peopleCounter.font = UIFont.systemFontOfSize(14) 
    } 

    peopleCounter.textAlignment = .Right 
    peopleCounter.textColor = Constants.Colors.ThemeDarkGray 

    addSubview(titleLabel) 
    addSubview(peopleCounter) 
    self.titleLabel.snp_makeConstraints { (make) -> Void in 
     make.centerY.equalTo(self) 
     make.height.equalTo(self) 
     make.width.equalTo(peopleCounter).multipliedBy(3) 
     make.left.equalTo(self).offset(titleLeftOffset) 
    } 

    self.peopleCounter.snp_makeConstraints { (make) -> Void in 
     make.centerY.equalTo(self) 
     make.height.equalTo(self) 
     make.width.equalTo(counterWidth) 
     make.left.equalTo(titleLabel.snp_right) 
     make.right.equalTo(self).offset(counterRightOffset) 
    } 
    } 

} 

कोड हैडर को पुनः प्राप्त करने के साथ एक साधारण शीर्षक है:

let mutualFriendsSectionView = ProfilePeopleListHeaderViewCell(title: Strings.Title.MutualFriends, numberOfPeople: 0) 

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    if section == 1 { 
     //mutualFriendsSectionView.layoutSubviews() 
     return mutualFriendsSectionView 
     } 
    } 

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 40 
    } 

मैं अनुभाग शीर्ष लेख में एक हरे रंग की पृष्ठभूमि मिलता है। लेकिन मैं किसी भी लेबल नहीं दिख रहा है ...

+0

कोड में बजाए आईबी में इस सेल को डिजाइन करना कहीं बेहतर और आसान होगा। – Shripada

+0

यदि आपके पास कोड संस्करण के लिए समाधान है। मेरी दिलचस्पी है। अन्यथा, मैं नहीं हूँ। धन्यवाद। – Mikael

+0

क्या आपने अपने कोड में 'numberOfSectionsInTableView' रखा था? –

उत्तर

0

नीचे के रूप में वास्तव में यह काम कर रहा है अपने कोड instantiating प्रयास करें। मेरी गलती यह मानना ​​था कि didSet {} को कक्षा के भीतर भी बुलाया गया था -> यह मामला नहीं है।

जब मैं init() में शीर्षक स्थापित कर रहा था, तो यह लेबल टेक्स्ट सेट नहीं कर रहा था।

4
Try this out for UITableView Section Header. 

1. Programatically create headerview 

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let headerView = UIView.init(frame: CGRectMake(0, 0, tblView.bounds.size.width, 50)) 
     let lblHeader = UILabel.init(frame: CGRectMake(15, 13, tableView.bounds.size.width - 10, 24)) 
     if section == 0 { 
      lblHeader.text = "Image Settings" 
     } 
     else if section == 1 { 
      lblHeader.text = "Personal Settings" 
     } 
     else { 
      lblHeader.text = "Other Settings" 
     } 
     lblHeader.font = UIFont (name: "OpenSans-Semibold", size: 18) 
     lblHeader.textColor = UIColor.blackColor() 
     headerView.addSubview(lblHeader) 
     headerView.backgroundColor = UIColor(colorLiteralRed: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1.0) 
     return headerView 
    } 

2. Height for HeaderView 

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50 
    } 
+0

क्या आप कोड बनाने के फ़ंक्शन का उपयोग कर सकते हैं? मैं तब आपके समाधान का प्रयास करूंगा :) – Mikael

+0

मैं अब सेक्शन हेडर देख सकता हूं लेकिन मुझे अभी भी समझ में नहीं आ रहा है कि क्यों मेरा प्रोफाइलपूलिस्टलिस्ट हैडरव्यूसेल क्लास काम नहीं करता है ... केवल एक अंतर जो मैं देखता हूं वह है कि आप ऑटोलायआउट के बजाय सीजीआरईटीमेक का उपयोग कर रहे हैं ... – Mikael

0

अपने headerView viewForHeader विधि के अंदर

संपादित

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
{ 

    let mutualFriendsSectionView = ProfilePeopleListHeaderViewCell(title: Strings.Title.MutualFriends, numberOfPeople: 0) 
    if section == 1 
    { 
     //mutualFriendsSectionView.layoutSubviews() 
     return mutualFriendsSectionView 
    } 
} 
+0

मुझे एक ही परिणाम मिलते हैं। एक हरा पृष्ठभूमि। – Mikael

संबंधित मुद्दे