Cenário:
Tenho um webservice que utilizo para pegar textos, estes textos podem ser modificados por uma interface web, mas o titulo deles sempre será fixo, por isto foi decidido a inserção deles diretamente na aplicação mobile.
O que estou fazendo atualmente:
Estou realizando uma requisição que transforma esse json em um array de Strings. Depois de fazer o loop e criar este Array Com Strings carrego uma tabela.
Agora, preciso redefinir este Array utilizando outro array fixo com a Seguinte Estrutura.
let textoFixo:[String] = ["Titulo 1", "Leia mais", "Titulo 2","Leia mais","Titulo 3"]
Sendo o Array da requisição:
let texto:[String] = ["conteudo1","conteudo2","conteudo3"];
Se tornando assim:
let texto:[String] = ["Titulo 1","conteudo1","Leia mais","Titulo 2","conteudo2","Leia mais"]
Sendo que o Titulo deve ter fonte maior em negrito e o leia mais deve ser direcionado para outra tela, que será estatica também podendo ser acionada por um pushViewController:
self.navigationController!.pushViewController(newViewController, animated: true)
Implementação:
import UIKit
import MMDrawerController
import SwiftyJSON
class FooViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var texto = [String]()
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
let load = Loader(view: self.view) // progress loader
table.delegate = self
table.dataSource = self
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = 44.0
let api = API()
api.get("foo") { responseObject, error in // request json data
let value = JSON(responseObject!)
for (_, subJson) in post { // fill array of strings
self.texto.append(subJson.stringValue)
}
self.table!.reloadData() // load de table with the data
load.stop(self.view)
}
}
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 = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.texto[indexPath.row]
return cell
}
}
Pode ocorrer da estrutura ser quebrada se existir dois conteudos sendo cada um deles um paragrafo.