Ahmad Mahamid Ahmad Mahamid - 6 months ago 7
Swift Question

xml parseing using Swift (IOS) Help me

I have this XML By URL :

<NewDataSet>
<Table>
<ID>1</ID>
<SongName>AYA LIV LIVOKIM PEL?STANKTV</SongName>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song1.mp3</SongPath>
<SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo1.png</SongImagePath>
</Table>
<Table>
<ID>2</ID>
<SongName>DîLAN PPP PELISTANK</SongName>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song2.mp3</SongPath>
<SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo2.png</SongImagePath>
</Table>
<Table>
<ID>3</ID>
<SongName>KARIN BAL DAGRIM</SongName>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song3.mp3</SongPath>
<SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo3.png</SongImagePath>
</Table>
<Table>
<ID>4</ID>
<SongName>RUKEN WERE CANE</SongName>
<SongPath>http://jo.sms2tv.com/PelistankApp/Songs/song4.mp3</SongPath>
<SongImagePath>http:\\jo.sms2tv.com\PelistankApp\Images\logo4.png</SongImagePath>
</Table>
</NewDataSet>


I want Show the Song Name in the table and i maked it no problem ,
now i want when i clicked in a cell on the Table Give me the Song Path And Put it in NSURL , because i want use it to PLAY Song .
1 - I Show the SongName in the Table no problem .
2 - I know how make AVPlayer To PLAY Song
Just i want when clicked on the CELL Give me the SongPath and put it in NSURL

And This is my Swift Code :

class ViewController: UIViewController, NSXMLParserDelegate, UITableViewDataSource, UITableViewDelegate
{

@IBOutlet var tbData : UITableView?

var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()

override func viewDidLoad()
{
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
self.beginParsing()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func beginParsing()
{

posts = []
parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://jo.sms2tv.com/PelistankApp2/getsongs.aspx"))!)!
parser.delegate = self
parser.parse()

tbData?.reloadData()
}


//////////////////////////////////////XMLParser Methods

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("Table")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
date = NSMutableString()
date = ""
}
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("Table") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "date")
}

posts.addObject(elements)
}
}

func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("SongName") {
title1.appendString(string)
} else if element.isEqualToString("SongPath") {
date.appendString(string)
}
}
///////////////////////////////////////////XMLParser Methods


//////////////////////////////Tableview Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")!

if(cell.isEqual(NSNull)) {
cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil) [0] as! UITableViewCell
}

cell.textLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
cell.detailTextLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String

return cell as UITableViewCell
}


//////////////////////////////////////Tableview Methods


/////// Table Action ( Cell clicked ) ///////

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)

let row = indexPath.row
print("Row: \(row)")

}

/////// Table Action ( Cell clicked ) ///////


@IBAction func Song(sender: UIButton) {

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewSong")
self.presentViewController(nextViewController, animated:true, completion:nil)

}

@IBAction func BackTableToHome(sender: UIBarButtonItem) {

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("Home")
self.presentViewController(nextViewController, animated:true, completion:nil)
}

//////////Button SecandViewController ////
@IBAction func SecondViewController(sender: AnyObject) {

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewSong")
self.presentViewController(nextViewController, animated:true, completion:nil)
}
}


And This is my Table

enter image description here

Any advice for this issue
Thank's

Answer

Here is what you wanted it prints the URL on tap.

    /////// Table Action ( Cell clicked ) ///////

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        let row = indexPath.row
        print("Row: \(row)")

        print(posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String) //Prints the song url

    }

Here is the project link https://drive.google.com/file/d/0B2csGr9uKp1DN1VodkNndmtQR2c/view?usp=sharing

Comments