asked    Gabriel     2018-10-22       ios       18 view        1 Answer

[SOLVED] UITableView does not show data correctly in my custom cell UILabel and reuse problem

I create my custom cell class and design my user interface with the code and constraints (I have 2 UILabels that hold my data in each row). I register my customcell class in the main viewController and load data in the tableview.

The problem is one of my UILabels fills with data but the second one not show its data until I scroll the table view or click right on that row. See images.

Before Scroll

enter image description here

After scroll

enter image description here

Here is my code:

class ProjectDetailsCustomCell: UITableViewCell {
    var key   : String?
    var value : String?

    var keyContainerView : UIView = {
        var containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = UIColor(red: 0.867, green: 0.867, blue: 0.867, alpha: CGFloat(1))
        return containerView
    }()

    var keyView : UILabel = {
        var keyTextView = UILabel()
        keyTextView.translatesAutoresizingMaskIntoConstraints = false
        keyTextView.font = UIFont(name: "IRANSansWeb", size: 13)
        keyTextView.textAlignment = .center
        return keyTextView
    }()

    var valueView : UILabel = {
        var valueTextView = UILabel()
        valueTextView.translatesAutoresizingMaskIntoConstraints = false
        valueTextView.textAlignment = .center
        valueTextView.font = UIFont(name: "IRANSansWeb", size: 12)
        //valueTextView.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: CGFloat(1))
        return valueTextView
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(valueView)
        self.addSubview(keyContainerView)
        keyContainerView.addSubview(keyView)
        self.backgroundColor = UIColor.clear
        self.heightAnchor.constraint(equalToConstant: 42).isActive = true
        keyContainerView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        keyContainerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        keyContainerView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.4).isActive = true
        keyContainerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
        keyView.rightAnchor.constraint(equalTo: self.keyContainerView.rightAnchor).isActive = true
        keyView.topAnchor.constraint(equalTo: self.keyContainerView.topAnchor).isActive = true
        keyView.bottomAnchor.constraint(equalTo: self.keyContainerView.bottomAnchor).isActive = true
        keyView.widthAnchor.constraint(equalTo: self.keyContainerView.widthAnchor).isActive = true
        valueView.rightAnchor.constraint(equalTo: self.keyContainerView.leftAnchor).isActive = true
        valueView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        valueView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        valueView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.6).isActive = true
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        if let key = key {
            keyView.text = key + value!
        }

        if let value = value {
            valueView.text = value
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

here is my data source code that populate the data array :

for i in 0..<self.detailsArray.count {
            // Populate our array list with the real data and restart the tableview
            self.dict = self.detailsArray[i]
            self.dataList.append(DetailsCellData.init(key: "شماره سفارش", value: String(self.dict["Id"] as! Int)))
            self.dataList.append(DetailsCellData.init(key: "قیمت", value: self.dict["Price"] as? String))
            self.dataList.append(DetailsCellData.init(key: "زمان و تاریخ سفارش", value: self.dict["DateTime"] as? String))
            self.dataList.append(DetailsCellData.init(key: "نام و نام خانوادگی کارفرما", value: self.dict["UserInformation"] as? String))
            self.dataList.append(DetailsCellData.init(key: "شماره تماس کارفرما", value: self.dict["UserPhone"] as? String))
            self.dataList.append(DetailsCellData.init(key: "نام خدمات دهنده", value: self.dict["ContractorInformation"] as? String))
            self.dataList.append(DetailsCellData.init(key: "شماره خدمات دهنده", value: self.dict["ContractorPhone"] as? String))
            self.dataList.append(DetailsCellData.init(key: "تاریخ درخواست خدمات", value: self.dict["ServiceDate"] as? String))
            self.dataList.append(DetailsCellData.init(key: "ساعت درخواست خدمات", value: self.dict["ServiceTime"] as? String))
            self.dataList.append(DetailsCellData.init(key: "مقدار کار", value: (self.dict["WorkAmmount"] as? String)! + " " + (self.dict["WorkUnit"] as? String)!))
            self.dataList.append(DetailsCellData.init(key: "هزینه تخمینی (ریال)", value: self.dict["EstimatedPrice"] as? String))
            self.tableview.reloadData()

here is my table view delegates code

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (dataList.count < 11){
        return 0
    } else {
        return dataList.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "detailCustomCell", for: indexPath) as! ProjectDetailsCustomCell
    cell.key = self.dataList[indexPath.row].key
    cell.value = self.dataList[indexPath.row].value
    return cell
}

  1 Answer  

        answered    Jane     2018-10-22      

override func layoutSubviews() {
    super.layoutSubviews()

    updateLayout()
}

func updateLayout() {
    if let key = key {
        keyView.text = key + value!
    }

    if let value = value {
        valueView.text = value
    }
}

try this way, also add a line here

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "detailCustomCell", for: indexPath) as! ProjectDetailsCustomCell
    cell.key = self.dataList[indexPath.row].key
    cell.value = self.dataList[indexPath.row].value
    cell.updateLayout()
    return cell
}




Your Answer





 2018-10-22         Rory

UseWebpackDevMiddleware AggregateException

I try to create a ASP.NET Core MVC application using NPM/Webpack/Vue.js. I am following this tutorial:https://marczak.io/posts/netcore-vuejs/When starting the project I get this exception:An exception of type 'System.AggregateException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'One or more errors occurred.' Inner exceptions found, see $exception in variables window for more details. Innermost exception Newtonsoft.Json.JsonReaderException : Unexpected character encountered while parsing value: <. Path '', line 0, position 0. at Newton...
 c#                     1 answers                     92 view
 2018-10-22         Riva

RenderBody with jQuery Ajax calls

I'm think that I'm doing something wrong here in .Net Core. I have layout with @RenderBody Problem is - I'm doing jQuery Ajax calls, my Actions returning me Partial Views. That works good. Problem is, I don't see any path of Controller and Action in URL, can't refresh page, I am returned to the Home. Another problem with Partials Views I can't solve is - Scripts:I can include all scripts in Layout, but some partials views requiring 2-3 scripts, another partials views different 2-3 scripts. I can't split usage, I don't want to throw all in one layout.Can't add them in the Pa...
 c#                     1 answers                     96 view
 2018-10-22         Calvin

c# Count rows in DataTable where all values of row equal to 1

I have a datatable in which the columns and rows are created at runtime, so as a result the column names may vary and row numbers too. Values for each cell can only be 1 or 0.I may get a datatable with say for example 3 columns. e.g.Age | Sex | Location-------------------1 | 0 | 1-------------------0 | 1 | 0--------------------1 | 1 | 1--------------------0 | 1 | 0--------------------1 | 1 | 1--------------------I want to be able to count only the rows which contain 1 on each column. So above example would return 2. Iterate through the rows and column...
 c#                     1 answers                     101 view