How to customize Uitableview?

Asked

Viewed 997 times

1

I’m populating a UITableView with data that I pick up on a Webservice JSON.

These data are texts that I need to format.

It’s coming this way:

inserir a descrição da imagem aqui

My Viewcontroller:

//
//  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
  }

}

Example source: Github

1 - How can I accomplish 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?

2 answers

2


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

  • Activate Size Classes in the Storyboard views. Because then we can work independent of the screen size of the user. Size Classes Apple

    inserir a descrição da imagem aqui


  • Setar the Size of its Viewcontroller to the size of the iPhone 5.5-inch. This is a tip to apply the design of your screen using as a reference the iPhone 6 screen+

    inserir a descrição da imagem aqui


  • Select your Tableview and let it the view size: 414x736, then put the Row Height as 100, finally add the contraints in the margins.

inserir a descrição da imagem aqui


  • Your file ViewController.swift will look like this: I explained the modifications in the comments

//
//  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 Storyboard Cell by telling her size and what class she is.

    inserir a descrição da imagem aqui

    inserir a descrição da imagem aqui

    inserir a descrição da imagem aqui


  • Finally adjust the properties of label in Storyboard:

    - Add label inside Cell

    inserir a descrição da imagem aqui

    - Set the label contraints

    inserir a descrição da imagem aqui

- Set custom text font

inserir a descrição da imagem aqui

- Set label line properties

inserir a descrição da imagem aqui

- Connect the storyboard label to the class label

inserir a descrição da imagem aqui


The result looks like this on the screen

inserir a descrição da imagem aqui

  • +1 exceptional, I saw the notification of github, tomorrow I will pick up the Mac and I will do all the tests, your reply besides helping me will help a lot of people because the details mentioned can be applied in various things, issue of size class and apply contraints they are defined in the main project in this was only example.

  • Doubt, use tableview and the most recommended way to fill out information on the screen ? I say this because I saw from the web and there we use table only for tabulated data and Section for texts and these details understand?

  • 1

    It’s usually @Gabrielrodrigues. Table Views and Collection Views are used a lot when you need to do something like this. You may be looking at what the Apple talks about Table Views, in fact recommend you always consult the Human Interface Guide for iOS because it is a document that helps a lot.

1

To disable the click of a specific cell:

cell.selectionStyle = UITableViewCellSelectionStyle.None

To change the source you can change by 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 Cell prototype, as I answered in this another question:

Just drag to prototype Cell from your table the Uilabels you need (in your case text, title, etc).

It works as a template for table rows, so you can create a custom class you inherit from Uitableviewcell to link to @Iboutlet, or access via Tags.

Reference:

Uicollectionview

Uitableview

Tutorial Prototype Cells:

http://www.raywenderlich.com/113388/storyboards-tutorial-in-ios-9-part-1

  • +1 top, these settings should be in the tableview method - > cellForRowAtIndexPath ? there I enter the value in Cell. I will do some tests!

  • @Gabrielrodrigues yes, should be in the method "cellForRowAtIndexPath"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.