Is there a way to stylize a UITableViewCell in swift?

I have a table view and I add to it several cells based on my json.

So far, the code for adding cells is as follows:

override func viewWillAppear(animated: Bool) {
    let frame:CGRect = CGRect(x: 0, y: 90, width: self.view.frame.width, height: self.view.frame.height-90)
    self.tableView = UITableView(frame: frame)
    self.tableView?.dataSource = self
    self.tableView?.delegate = self
    self.view.addSubview(self.tableView!)

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("CELL")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    }

    let user:JSON =  JSON(self.items[indexPath.row])

    cell!.textLabel?.text = user["description"].string

    var photoURL =  "/path/to/my/icon/google.png"

    if let data = NSData(contentsOfFile: photoURL)
    {

        cell!.imageView?.image = UIImage(data: data)
    }
    return cell!
}

Apart from descriptionmy json I also usernameand price. So far - since I add only imageViewand description, 3 cells look like this:

enter image description here

Is there a way to style it so that each cell looks something like this:

enter image description here

( priceand usernamehere are gray)? How can I achieve this effect?

=== EDIT:

this is how i fill the table:

I am retrieving data from a leisure web service in json:

func getAllUsers() {
    RestApiManager.sharedInstance.getUsers { json in
        let results = json
        for (index: String, subJson: JSON) in results {
            let user: AnyObject = JSON.object
            self.items.addObject(user)
            dispatch_async(dispatch_get_main_queue(),{
                self.tableView?.reloadData()
            })
        }
    }
}

and I call this method in my viewWillAppearfunction

+4
source share
3
  • . imageView, storyborad. enter image description here

  • UITableViewCell. , , ExampleTableViewCell. . enter image description here

  • ExampleTableViewCell. enter image description here

  • imageView ExampleTableViewCell. ExampleTableViewCell. enter image description here

  • - cellForRowAtIndexPath. var. ExampleTableViewCell. , ExampleTableViewCell . , resuseIdentifier . , , .

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! ExampleTableViewCell
    
    cell.imageDisplay.image = yourImage
    cell.descriptionLabel.text = yourDescription
    cell.priceLabel.text = yourPrice
    cell.usernameLabel.text = yourUsername
    
    return cell
    

    }

+2

UITableViewCells .

, Storyboard, , , UITableViewCell . cellForRowInIndexPath , UITableViewCells.

. http://shrikar.com/uitableview-and-uitableviewcell-customization-in-swift/

+3

UITableViewCell. TableView , - , ctrl + / UITableViewCell , .

+1

Source: https://habr.com/ru/post/1629734/


All Articles