How to add text at specific position in String Array

Asked

Viewed 140 times

1

Setting:

I have a webservice that I use to pick up texts, these texts can be modified by a web interface, but the title of them will always be fixed, so it was decided to insert them directly into the mobile application.

What I’m currently doing:

I’m making a request that turns this json into an array of strings. After loop and create this String Array I load a table.

Now, I need to reset this Array using another fixed array with the Next Structure.

let textoFixo:[String] = ["Titulo 1", "Leia mais", "Titulo 2","Leia mais","Titulo 3"]

Being the Request Array:

let texto:[String] = ["conteudo1","conteudo2","conteudo3"];

Thus becoming:

let texto:[String] = ["Titulo 1","conteudo1","Leia mais","Titulo 2","conteudo2","Leia mais"]

Since Titulo must have larger font in bold and read it more should be directed to another screen, which will be static and can also be triggered by a pushViewController:

self.navigationController!.pushViewController(newViewController, animated: true)

Implementation:

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

The structure may be broken if there are two contents each of which is a paragraph.

  • Check this to see if this is what you want and help you... http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-swift

1 answer

0


You can use the Insert method by passing as the second parameter the position of the array to be inserted.

Example:

texto.insert(textoFixo[0], atIndex: 0);
texto.insert(textoFixo[1], atIndex: 2);

Browser other questions tagged

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