How to customize UITableView?

1

I'm populating a UITableView with data I get in a WebService JSON.

This data is text that I need to format.

It's coming this way:

MyViewController:

//
//  ViewController.swift
//  tableView
//
//  Created by Gabriel Rodrigues on 03/12/15.
//  Copyright © 2015 Sephirot. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  var texto: [String] = []@ IBOutlet weak
  var table: UITableView!

    override func viewWillAppear(animated: Bool) {
      self.table.reloadData()
    }

  override func viewDidLoad() {
    super.viewDidLoad()
    table.delegate = self
    table.dataSource = self

    loadPosts()
  }

  func loadPosts() {
    let url = "http://puc.vc/painel/webservice/procedimentosacademicos/"
    Alamofire.request(.GET, url)
      .responseJSON {
        response in

          if
        let value: AnyObject = response.result.value {
          let post = JSON(value)
          for (_, subJson) in post {
            self.texto.append(subJson.stringValue)
          }
        }

        dispatch_async(dispatch_get_main_queue(), {
          self.table!.reloadData()
        })
      }
  }

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

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

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel ? .text = self.texto[indexPath.row]
    print(self.texto[indexPath.row])
    return cell
  }

}

Source Example: Github

1 - How can I perform line breaks in large texts?

2 - How can I remove the click event on these lines? or specify a specific one to have the click.

3 - How to define a custom font for this UITableViewCell ?

    
asked by anonymous 04.12.2015 / 01:30

2 answers

2

I made a pull request in your repository with the solution below:

  • Enable Size Classes in Storyboard views. So we can work independently of the size of the user's screen. Size Classes Apple

  • SettheSizeofyourViewControllertothesizeofiPhone5.5-inch.ThisisatiptoapplythedesignofyourscreenusingtheiPhone6+screenasareference

  • SelectyourTableViewandleaveittheviewsize:414x736,thensetRowHeightto100,thenaddthecontraintsonthemargins.

  • YourViewController.swiftfilelookslikethis:Iexplainedthemodificationsinthecomments

//
//  ViewController.swift
//  tableView
//
//  Created by Gabriel Rodrigues on 03/12/15.
//  Copyright © 2015 Sephirot. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  var texto: [String] = []@ IBOutlet weak
  var table: UITableView!

    override func viewWillAppear(animated: Bool) {
      self.table.reloadData()
    }

  override func viewDidLoad() {
    super.viewDidLoad()
    table.delegate = self
    table.dataSource = self
    
    // essas propriedades falam pra sua tableView que o tamanho da row será automático mas que o esperado é 100
    table.estimatedRowHeight = 100 
    table.rowHeight = UITableViewAutomaticDimension

    loadPosts()
  }

  func loadPosts() {
    let url = "http://puc.vc/painel/webservice/procedimentosacademicos/"
    Alamofire.request(.GET, url)
      .responseJSON {
        response in

          if
        let value: AnyObject = response.result.value {
          let post = JSON(value)
          for (_, subJson) in post {
            self.texto.append(subJson.stringValue)
          }
        }

        dispatch_async(dispatch_get_main_queue(), {
          self.table!.reloadData()
        })
      }
  }

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

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

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
    
    // agora sua cell será do tipo CustomTableViewCell
    let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell

    // setando o texto na label textoBasico
    cell.textoBasico.text = self.texto[indexPath.row]
    print(self.texto[indexPath.row])
    return cell
  }

}

// Classe que herda de UITableViewCell para ser sua custom cell
class CustomTableViewCell: UITableViewCell {
   @IBOutlet var textoBasico: UILabel!
}
  • Change the cell of the Storyboard by talking about its size and what class it is.

  • FinallyadjustthepropertiesoflabelintheStoryboard:

    -Addthelabelinsidethecell

    -Setthelabel'scounterints

-Setthecustomtextfont

-Setthelinepropertiesofthelabel

-Connectthestoryboardlabelwiththeclasslabel

Theresultlookslikethisonthescreen

    
06.12.2015 / 02:15
1

To disable the click of a specific cell:

cell.selectionStyle = UITableViewCellSelectionStyle.None

To change the font you can change through MainStoryBoard, or via code

cell.textLabel.font = UIFont(name: "ArialMT", size: 16)

Line breaks and number of lines in the same way:

cell.textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel.numberOfLines = 3

If you have separate items (more than one text) for each cell you can create a prototype cell, as I replied in this another question :

Just drag the UILabels you need (in your case text, title, etc.) into the prototype cell of your table.

It works as a template for table rows, so you can create a custom class that inherits from UITableViewCell to bind with @IBOutlet, or access via Tags.

Reference:

UICollectionView

UITableView

Prototype Cells tutorial:

link

    
04.12.2015 / 19:52