1

I have a API that hands back a string of numbers based on a User. Once the API Request is made the function places the numbers into an Array.

var usernumbers = userResponse["Numbers"] as NSArray

The debugger PrintLn shows:

( 16467621007, 14152555613, 14046206007 )

When I hand this to UITableView, how do I split up the array so that each number goes into a separate cell? Ive tried:

let rowData: NSArray = tableData (usernumbers has been handed to table data)

I've tried:

cell.textLabel?.text = rowData.row (and all varieties of)

Where am I going wrong?

1
  • Can you show the implementation of your cellForRowAtIndexPath and numberOfRowsInSection methods? And you should be getting an error from rowData.row, since NSArray doesn't have a row property. Commented Mar 14, 2015 at 18:24

3 Answers 3

1

If i understood your problem correctly, this is what you are looking for,

cell.textLabel?.text = rowData[indexPath.row] as String

Edit:

let numberValue : NSNumber = rowData[indexPath.row] as NSNumber

 let text : String = numberValue.stringValue // add  nil check

 cell.textLabel?.text = text 

You can directly assign like

cell.textLabel?.text = numberValue.stringValue - if you are sure there won't be a nil value

Sign up to request clarification or add additional context in comments.

2 Comments

When I use 'rowData[indexPath.row] as String' I get "Swift dynamic cast failed" so Im not converting from the Array into a string correctly.
Currently the func tableView code is: let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell") let rowData: NSArray = tableData cell.textLabel?.text = rowData[indexPath.row] as String return cell which fails on a 'Swift dynamic cast failed' error
0

Use rowData[indexPath.row] where indexPath.row is the current cell starting from 0

Comments

0
 private let data = Array(1...9)

then...

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

and in your :

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

all the usual stuff.

  cell.textLabel?.text = String(format: "the magic number is : %.f", data[indexPath.row])
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.