Almudhafar
Almudhafar

Reputation: 887

Array with key in swift

i am using this code to get text from array with key

This code in view did load

    if let rs = db.executeQuery("select * from myTable", withArgumentsInArray: nil) {

        while rs.next() {

            let id = rs.stringForColumn("id")
            let text1 = rs.stringForColumn("text")

            dataArray.addObjectsFromArray(NSArray(objects:NSDictionary(object:NSArray(objects: id, text1), forKey:NSArray(objects: "id", "texti"))))



        }

    } else {
        println("executeQuery failed: \(db.lastErrorMessage())")
    }

...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = dataArray[indexPath.row].objectForKey("texti") as? String

    return cell
}

The cell label text is null, why?


    println("dataArray: \(dataArray)")

dataArray: (
        {
                (
            id,
            texti
        ) =         (
            1,
            John
        );
    },
        {
                (
            id,
            texti
        ) =         (
            2,
            Mark
        );
    },
        {
                (
            id,
            texti
        ) =         (
            3,
            Marry
        );
    }
)

Is there a problem in this? I am using same code in objective-c and its working probably

Upvotes: 0

Views: 495

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

Call self.tableView.reloadData() after you retrieve the dataArray. Change the dataArray.addObjects line to

dataArray.addObject(["id":id,"texti" : text1])

if let rs = db.executeQuery("select * from myTable",    withArgumentsInArray: nil) {

    while rs.next() {

        let id = rs.stringForColumn("id")
        let text1 = rs.stringForColumn("text")

        dataArray.addObject(["id":id,"texti" : text1]) // Added Line
    }
    self.tableview.reloadData() //Added Line

} else {
    println("executeQuery failed: \(db.lastErrorMessage())")
}

Inside cellForRowAtIndexPath

if let txt =  dataArray[indexPath.row].objectForKey("texti") as? String
{
    cell.textLabel?.text = txt
}

Upvotes: 1

Related Questions